Results 1 to 3 of 3

Thread: [SOLVED] Automatic children freeing for objects derived from QObject

  1. #1
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default [SOLVED] Automatic children freeing for objects derived from QObject

    Hello again people,

    I have created long time ago some code which overloads new, new[], delete and delete[] operators, and which keeps track of allocated / freed memory in a std::map stuff.

    My problem is, that i'm trying to use my code in my new QT application, and the result is that i get some warnings on memory leaks which i don't understand:
    for instance, if in my main window object i do:
    Qt Code:
    1. QTreeWidgetItem* pItmToAdd = new QTreeWidgetItem(pParent);
    To copy to clipboard, switch view to plain text mode 
    Do i have to delete it then? Or will QT handle this itself when "destructing" the parent QTreeWidget?
    I have the very same problem each time i "new" an object which herits of QObject:
    I have made some little KIO-like listers which are all derived from a class IFilesLister. So i get CFilesLister_File, CFilesLister_FTP, etc... The thing is, that in my application i do construct them this way:
    Qt Code:
    1. IFilesLister* pLocalLister = new CFilesLister_FTP();
    To copy to clipboard, switch view to plain text mode 
    and in the main window's destructor i do:
    Qt Code:
    1. delete pLocalLister;
    To copy to clipboard, switch view to plain text mode 
    Then, my log shows the constructor AND destructor as well:
    Qt Code:
    1. Dbg> -> An [_FTP] Files Lister object is being constructed.
    2. Dbg> Creating an instance of the internal FTP object...
    3. Dbg> The [_FTP] Files Lister is initializing it's slots...
    4. Dbg> <- The [_FTP] Files Lister object was created.
    To copy to clipboard, switch view to plain text mode 
    Then later:
    Qt Code:
    1. Dbg> -> Destructing the main application Dialog.
    2. Dbg> -> The [_FTP] Files Lister object is destroying...
    3. Dbg> <- The [_FTP] Files Lister object was destroyed.
    4. Dbg> <- Done destructing the main application Dialog.
    To copy to clipboard, switch view to plain text mode 

    So i have the "debug" proof that my objects get actually destroyed, but my memory leaks tracking system still anoys me with warnings on those pointers...

    The only explaination i have is, that because the objects pointed by my leaks manager are all derived from QObject, maybe QT delete them itself before...

    Any idea on how to solve this?
    Bye!
    Pierre.

    [EDIT:]
    Here is a sample output of my leaks tracking system:
    Qt Code:
    1. Dbg> -> Probable Memory Leaks Listing:
    2. Dbg> Src/CWndMainImpl.cpp:42 0x136736944, 36 byte(s).
    3. Dbg> Src/CWndMainImpl.cpp:33 0x136738728, 20 byte(s).
    4. Dbg> Src/CWndMainImpl.cpp:64 0x136816616, 32 byte(s).
    5. Dbg> Src/CWndMainImpl.cpp:72 0x136818288, 32 byte(s).
    6. Dbg> Src/CWndMainImpl.cpp:90 0x136820016, 32 byte(s).
    7. Dbg> Src/CWndMainImpl.cpp:98 0x136821392, 32 byte(s).
    8. Dbg> <- Total: 6 non-freed block(s), 184 byte(s)
    To copy to clipboard, switch view to plain text mode 
    Which ALL are "new"ed objects, either "native" QT or derivated from QObject...
    Last edited by hickscorp; 6th December 2006 at 23:55.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Automatic children freeing for objects derived from QObject

    Quote Originally Posted by hickscorp View Post
    will QT handle this itself when "destructing" the parent QTreeWidget?
    Yes, it will:
    Qt Code:
    1. #include <QObject>
    2. #include <QtDebug>
    3.  
    4. class Test : public QObject
    5. {
    6. public:
    7. Test( QObject * parent = 0 ) : QObject( parent ) { qDebug() << "Test::Test()"; }
    8. ~Test() { qDebug() << "Test::~Test()"; }
    9. };
    10.  
    11. int main()
    12. {
    13. Test *t1 = new Test();
    14. Test *t2 = new Test( t1 );
    15.  
    16. delete t1;
    17.  
    18. return 0;
    19. }
    To copy to clipboard, switch view to plain text mode 
    Result:
    Test::Test()
    Test::Test()
    Test::~Test()
    Test::~Test()

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

    hickscorp (6th December 2006)

  4. #3
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Automatic children freeing for objects derived from QObject

    Well...

    Unless i explicitely delete pLocalLister, i never get destructor debug from my objects.
    Let me summarize how they are made:
    Qt Code:
    1. class IFilesLister : public QObject {
    2. Q_OBJECT
    3. public:
    4. IFilesLister(void);
    5. virtual ~IFilesLister(void);
    6. // The following method calls "DoFetchContent" which is virtual...
    7. bool FetchContent(const CNodeDesc& cParent, QObject* pUserData=0);
    8. protected:
    9. virtual bool DoFetchContent(const CNodeDesc& cParent, QObject* pUserData) = 0;
    10. };
    To copy to clipboard, switch view to plain text mode 

    And then:

    Qt Code:
    1. class CFilesLister_File : public IFilesLister {
    2. Q_OBJECT
    3. public:
    4. CFilesLister_File(void);
    5. CFilesLister_File(const QString& cBasePath_p);
    6. virtual ~CFilesLister_File(void);
    7. protected:
    8. virtual bool DoFetchContent(const CNodeDesc& cParent, QObject* pUserData);
    9. };
    To copy to clipboard, switch view to plain text mode 

    So, what am i doing wrong here?

    [EDIT:] Wooops sorry my misstake, my objects don't "register" to their parent
    Sorry for this time.

    Thanks!
    Pierre.
    Last edited by wysota; 7th December 2006 at 00:08. Reason: reformatted to look better

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.