Results 1 to 5 of 5

Thread: QString, QStringList and garbage collection...

  1. #1
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QString, QStringList and garbage collection...

    Hi!

    Are QString and QStringList garbage collected garbage collected?

    I need to manipulate a QString using both QString methods and a QStringList and I'm wondering if I have to do any cleanup after I processed the QString passed as a parameter...

    Thank you!

    Nick

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QString, QStringList and garbage collection...

    If you create it on the heap, then you have to care about. But since they are implicit shared, simple create them on the stack and all is fine:
    Qt Code:
    1. void Class::function()
    2. {
    3. QString str;
    4. str = "foo";
    5. myFunctionTakingAString(str);
    6. // or
    7. emit mySignal(str);
    8. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Lykurg for this useful post:

    PaladinKnight (2nd April 2010)

  4. #3
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString, QStringList and garbage collection...

    Hi!

    Thank you for your help!

    Quote Originally Posted by Lykurg View Post
    If you create it on the heap, then you have to care about. But since they are implicit shared, simple create them on the stack and all is fine:
    I am also a newbie in C++ so I want to make sure I correctly understood what you said.

    (I am however familiar with C, Java and assembly language and I am trying to adjust to C++ and Qt using the concepts I learned in these other languages).

    OK, from what I've understood creating it on the heap would mean using a "new" like so:

    Qt Code:
    1. QString test = new QString("This is a test");
    To copy to clipboard, switch view to plain text mode 

    Creating it on the stack would be like this:

    Qt Code:
    1. QString test = "This is a test";
    To copy to clipboard, switch view to plain text mode 

    But then I have also seen this, what does this do?:

    Qt Code:
    1. QString test = QString("This is a test");
    To copy to clipboard, switch view to plain text mode 

    (Since there is no new I would assume it does the same thing as the previous one and some people seem to prefer declaring it that way.)

    The method I am calling looks something like this (I've simplified it from what it actually is):

    Qt Code:
    1. QString makeTest(QString str)
    2.  
    3. // Do some substitution..
    4. str.replace("*", QString("!"));
    5.  
    6. // Split at the # and trim what's between them
    7. QStringList sl = str.split('#');
    8. QStringList::iterator it;
    9.  
    10. for (it = sl.begin(); it != sl.end(); ++it)
    11. {
    12. (*it) = (*it).trimmed();
    13. }
    14.  
    15. // rejoin everything and put it back in the original string
    16. str = sl.join(" ");
    17.  
    18. // process the string further once it's merged back
    19. // (it's not exactly like that but it does give an idea
    20. // of the kind of further processing done here... Yep, mapping
    21. // the @ back to the separator used above is done on purpose...)
    22.  
    23. str.replace(QString("@"), QString("#"));
    24.  
    25. return text;
    26. }
    To copy to clipboard, switch view to plain text mode 

    (BTW, from what I have read QStringList is also implicitly shared,)

    I don't want to pass the QString by reference as I don't want the processing I'm doing to interfere with the rest of the treatment.

    If I understood your reply correctly the method I have shown above (probably) only uses the stack so it should free all of the memory it uses when it terminates, right?? What about the return parameter, is it freed when the calling method terminates?

    There is something about implicit sharing I'm not sure I understand though... Should it be able to free the memory allocated even if I had done a "new QString" since it apparently maintains a count of how many referrences there is to the object ?

    Thank you very much for your help!

    Nick

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QString, QStringList and garbage collection...

    Quote Originally Posted by PaladinKnight View Post
    OK, from what I've understood creating it on the heap would mean using a "new" like so:

    Qt Code:
    1. QString test = new QString("This is a test");
    To copy to clipboard, switch view to plain text mode 

    Creating it on the stack would be like this:

    Qt Code:
    1. QString test = "This is a test";
    To copy to clipboard, switch view to plain text mode 

    But then I have also seen this, what does this do?:

    Qt Code:
    1. QString test = QString("This is a test");
    To copy to clipboard, switch view to plain text mode 

    (Since there is no new I would assume it does the same thing as the previous one and some people seem to prefer declaring it that way.)
    Right. The difference is that one time Qt automatically converts char* to a QString and in the later you just call the constructor direct. You can run into trouble when setting QT_NO_CAST_FROM_ASCII. Then the first will not work. But if you don't set it, no difference at all.

    The method I am calling looks something like this (I've simplified it from what it actually is):

    Qt Code:
    1. QString makeTest(QString str)
    2.  
    3. // Do some substitution..
    4. str.replace("*", QString("!"));
    5.  
    6. // Split at the # and trim what's between them
    7. QStringList sl = str.split('#');
    8. QStringList::iterator it;
    9.  
    10. for (it = sl.begin(); it != sl.end(); ++it)
    11. {
    12. (*it) = (*it).trimmed();
    13. }
    14.  
    15. // rejoin everything and put it back in the original string
    16. str = sl.join(" ");
    17.  
    18. // process the string further once it's merged back
    19. // (it's not exactly like that but it does give an idea
    20. // of the kind of further processing done here... Yep, mapping
    21. // the @ back to the separator used above is done on purpose...)
    22.  
    23. str.replace(QString("@"), QString("#"));
    24.  
    25. return text;
    26. }
    To copy to clipboard, switch view to plain text mode 

    (BTW, from what I have read QStringList is also implicitly shared,)

    I don't want to pass the QString by reference as I don't want the processing I'm doing to interfere with the rest of the treatment.

    If I understood your reply correctly the method I have shown above (probably) only uses the stack so it should free all of the memory it uses when it terminates, right?? What about the return parameter, is it freed when the calling method terminates?
    Yes and no... The memory will (normally) be freed later:

    Qt Code:
    1. QString makeTest(QString str)
    2. {
    3. QString text; // here memory is allocated
    4. // do something
    5. return text; // Memory is not freed in normal case, because:
    6. }
    7.  
    8. //...
    9. QString returnValue = makeTest("foo"); // returnValue is taking over the memory of "text"
    To copy to clipboard, switch view to plain text mode 

    There is something about implicit sharing I'm not sure I understand though... Should it be able to free the memory allocated even if I had done a "new QString" since it apparently maintains a count of how many referrences there is to the object ?
    Ok, my answer was misleading. Implicit sharing (IS) has nothing to do with gc. if you use new you have to delete it. IS means only that different QStrings share data between and deep copies will only be made if really necessary. This is the reason why it is "safe" to pass QString as parameters to function because it is not really "by value". (Only if you change that string in your function of course)


    So hopefully to be a little bit clearer now: Qt is pure C++ after all and has no direct gc. Only if you have Classes with QObjects, then child items will be deleted if the parent is deleted:
    Qt Code:
    1. QObject *parent = new QObject;
    2. QWidget *child1 = new QWidget(parent);
    3. QPushButton *child2 = new QPushButton(parent);
    4. delete parent; // child1 and child2 will be deleted automatically!
    To copy to clipboard, switch view to plain text mode 
    For all other cases: if you use new, you have to delete them (as you do with parent). If you create any type on the stack, it will be deleted when the scope reaches the end. (and here IS comes into play: it is only deeply deleted if no other (e.g.) QString points to the shared memory.)


    Hope it is a little bit clearer now...

    Lykurg

  6. The following 2 users say thank you to Lykurg for this useful post:

    PaladinKnight (5th April 2010), soulless (5th April 2010)

  7. #5
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString, QStringList and garbage collection...

    Hi!

    Quote Originally Posted by Lykurg View Post
    Right. The difference is that one time Qt automatically converts char* to a QString and in the later you just call the constructor direct. You can run into trouble when setting QT_NO_CAST_FROM_ASCII. Then the first will not work. But if you don't set it, no difference at all.


    Yes and no... The memory will (normally) be freed later:

    Qt Code:
    1. QString makeTest(QString str)
    2. {
    3. QString text; // here memory is allocated
    4. // do something
    5. return text; // Memory is not freed in normal case, because:
    6. }
    7.  
    8. //...
    9. QString returnValue = makeTest("foo"); // returnValue is taking over the memory of "text"
    To copy to clipboard, switch view to plain text mode 
    Oops, that's weird... Until I tried to reply I couldn't see your code example... ???

    OK, even in this case the memory would eventually get freed though, right?

    Ok, my answer was misleading. Implicit sharing (IS) has nothing to do with gc. if you use new you have to delete it. IS means only that different QStrings share data between and deep copies will only be made if really necessary. This is the reason why it is "safe" to pass QString as parameters to function because it is not really "by value". (Only if you change that string in your function of course)
    OK...


    So hopefully to be a little bit clearer now: Qt is pure C++ after all and has no direct gc.
    Yep, I had read that C++ had no gc so that's why I was concerned about it... It's more or less like in good old C when you have/had to "free" memory you had allocated (using malloc, calloc, etc...).

    Only if you have Classes with QObjects, then child items will be deleted if the parent is deleted:
    Qt Code:
    1. QObject *parent = new QObject;
    2. QWidget *child1 = new QWidget(parent);
    3. QPushButton *child2 = new QPushButton(parent);
    4. delete parent; // child1 and child2 will be deleted automatically!
    To copy to clipboard, switch view to plain text mode 
    OK, thanks!

    For all other cases: if you use new, you have to delete them (as you do with parent). If you create any type on the stack, it will be deleted when the scope reaches the end. (and here IS comes into play: it is only deeply deleted if no other (e.g.) QString points to the shared memory.)
    OK, so that's why I had read IS had some influence in it...


    Hope it is a little bit clearer now...
    Yes, thank you very much!

    Have a nice day!

    Nick

Similar Threads

  1. QStringlist To QString
    By phillip_Qt in forum Qt Programming
    Replies: 6
    Last Post: 19th June 2015, 14:17
  2. QMAP<QString, QStringList>
    By bismitapadhy in forum Qt Programming
    Replies: 3
    Last Post: 4th February 2010, 04:47
  3. Q3ScrollView resists to scroll down to the garbage bin
    By sivrisinek in forum Qt Programming
    Replies: 0
    Last Post: 5th February 2009, 17:50
  4. question about garbage collection
    By Dumbledore in forum Qt Programming
    Replies: 4
    Last Post: 18th December 2007, 22:08
  5. Replies: 7
    Last Post: 2nd June 2006, 12:48

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.