Results 1 to 5 of 5

Thread: question about garbage collection

  1. #1
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    1

    Default question about garbage collection

    When you reimplement a class and allocate memory on the heap then delete the class with deleteLater() or whatever, do you need to create your own destructor for that class to free the allocated memory?

    links for recommended reading is appreciated!

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: question about garbage collection

    If your reimplementation adds additional members which you allocate on the heap then yes, you need to delete them in the destructor. Otherwise, all the memory gets freed when the object is deleted in the event loop.

    Recommended reading: http://www.qtcentre.org/forum/f-gene...html#post57155.

  3. #3
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    1

    Default Re: question about garbage collection

    haha thinking in C++ is a little less targeted than what I was expecting. But I suppose the message is that Qt doesn't add any garbage collection functionality? I only ask because in the examples I've seen there is never any reimplementation of the destructors to delete the memory allocated to the new members...

    For instance maybe something like this:

    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5. #include "fortuneserver.h"
    6.  
    7. class QLabel;
    8. class QPushButton;
    9.  
    10. class Dialog : public QDialog
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. Dialog(QWidget *parent = 0);
    16.  
    17. private:
    18. QLabel *statusLabel;
    19. QPushButton *quitButton;
    20. FortuneServer server;
    21. };
    22.  
    23. #endif
    To copy to clipboard, switch view to plain text mode 

    I don't see a specialized destructor and I'm just curious when I would need the destructor.

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: question about garbage collection

    That is because statusLabel and quitButton are created with this as parent, therefore they will be deleted when this gets destroyed. A QObject always deletes all its children.

    I was talking about about non-QObject members allocated on the heap. They have to be deleted manually.

  5. #5
    Join Date
    Jan 2006
    Location
    Earth (Terra)
    Posts
    87
    Thanks
    4
    Thanked 6 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: question about garbage collection

    Quote Originally Posted by Dumbledore View Post
    haha thinking in C++ is a little less targeted than what I was expecting. But I suppose the message is that Qt doesn't add any garbage collection functionality?
    If you're thinking like Java or C# or one of the other interpreted/VM-based languages, the answer is, "No, there is no garbage-collector." (The exception is something MS calls 'managed C++' in their .NET/cli framework - certainly not a standard C++.)

    Memory is allocated and freed explicitly. Some systems (like Qt) help you out a bit, like the example Marcel gives. You need to know that - not freeing a pointer means memory leaks, freeing an already freed pointer will cause much grief.

    rickb

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.