Results 1 to 4 of 4

Thread: Puzzled about how QString allocates memory

  1. #1
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Puzzled about how QString allocates memory

    Hello all,
    I'm using some C functions that expect old fashionned ascii string paths, but I'm using Qt utilities to handle the paths.
    Here is a sample static method I wrote:
    Qt Code:
    1. char *PathUtil::toWinAscii(QString fullpath)
    2. {
    3. QByteArray ba = fullpath.toLocal8Bit();
    4. char *s = ba.data();
    5. #if WIN32
    6. char *p = s;
    7. while(*p){if(*p == '/')*p = '\\'; p++;}// convert Qt's separators to Windows compatible separators
    8. #endif
    9. return s;
    10. }
    To copy to clipboard, switch view to plain text mode 

    Now if I do thousands of calls of this function (because I'm dealing with thousands of files), I expect to get a lot of memory eaten up.
    Should I free the memory allocated by data() with free or delete []?
    My guess is that I should get into the sources, but I still wonder whether this is discussed in the documentation.

    UPDATE:
    Sorry, but I am not sure if it really works at all. Is the value being returned treated as an "automatic variable", and disappear on return from the method?
    Last edited by feraudyh; 28th December 2013 at 18:53.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Puzzled about how QString allocates memory

    QByteArray::data() does not allocate new memory and make a copy, it merely provides a pointer the the QByteArray's internal buffer. This buffer will be valid as long as the QByteArray is in scope and not modified. If you want your own copy of this data then you need to make it yourself.

    In your example the QByteArray goes out of scope at the end of the function. at this point the internal buffer of the byte array is deallocated and potential reused by other memory allocations. Using your returned pointer after this will eventually (if not immediately) crash your program.

  3. #3
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Puzzled about how QString allocates memory

    Yes, I think you understood perfectly what I was assuming, which was wrong.
    I rewrote the signature and implementation as

    Qt Code:
    1. void PathUtil::toWinAscii(QString fullpath, char *destination)
    2. {
    3. QByteArray ba = fullpath.toLocal8Bit();
    4. char *s = ba.data();
    5. #if WIN32
    6. char *p = destination;
    7. while(*p){if(*p == '/')*p = '\\'; p++;}
    8. #endif*
    9. strcpy(destination, s);
    10. }
    To copy to clipboard, switch view to plain text mode 
    and assuming that destination is allocated this works fine.
    Thankyou
    Last edited by feraudyh; 29th December 2013 at 19:51. Reason: better formatting

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Puzzled about how QString allocates memory

    strcpy() is available on WIN32 as well, that's one thing. The other is that there is still no memory allocation here. If "destination" points to a buffer that is not big enough to hold the full path, you will end up with a buffer overflow.

    The easiest way to safely have the path with native separators is to use QDir::toNativeSeparators().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Qt 4.6.3 QString and memory consumption
    By webquinty in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 10th April 2012, 15:45
  2. QString memory consumption
    By sicker in forum Newbie
    Replies: 1
    Last Post: 4th November 2011, 16:23
  3. Replies: 2
    Last Post: 11th August 2011, 15:42
  4. Replies: 4
    Last Post: 31st January 2008, 20:44
  5. QString memory usage
    By jogeshwarakundi in forum Newbie
    Replies: 1
    Last Post: 13th December 2007, 06:48

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.