PDA

View Full Version : [SOLVED] Automatic children freeing for objects derived from QObject



hickscorp
6th December 2006, 23:11
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:

QTreeWidgetItem* pItmToAdd = new QTreeWidgetItem(pParent);
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:

IFilesLister* pLocalLister = new CFilesLister_FTP();
and in the main window's destructor i do:

delete pLocalLister;
Then, my log shows the constructor AND destructor as well:

Dbg> -> An [_FTP] Files Lister object is being constructed.
Dbg> Creating an instance of the internal FTP object...
Dbg> The [_FTP] Files Lister is initializing it's slots...
Dbg> <- The [_FTP] Files Lister object was created.
Then later:

Dbg> -> Destructing the main application Dialog.
Dbg> -> The [_FTP] Files Lister object is destroying...
Dbg> <- The [_FTP] Files Lister object was destroyed.
Dbg> <- Done destructing the main application Dialog.

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:

Dbg> -> Probable Memory Leaks Listing:
Dbg> Src/CWndMainImpl.cpp:42 0x136736944, 36 byte(s).
Dbg> Src/CWndMainImpl.cpp:33 0x136738728, 20 byte(s).
Dbg> Src/CWndMainImpl.cpp:64 0x136816616, 32 byte(s).
Dbg> Src/CWndMainImpl.cpp:72 0x136818288, 32 byte(s).
Dbg> Src/CWndMainImpl.cpp:90 0x136820016, 32 byte(s).
Dbg> Src/CWndMainImpl.cpp:98 0x136821392, 32 byte(s).
Dbg> <- Total: 6 non-freed block(s), 184 byte(s)
Which ALL are "new"ed objects, either "native" QT or derivated from QObject...

jacek
6th December 2006, 23:22
will QT handle this itself when "destructing" the parent QTreeWidget?
Yes, it will:
#include <QObject>
#include <QtDebug>

class Test : public QObject
{
public:
Test( QObject * parent = 0 ) : QObject( parent ) { qDebug() << "Test::Test()"; }
~Test() { qDebug() << "Test::~Test()"; }
};

int main()
{
Test *t1 = new Test();
Test *t2 = new Test( t1 );

delete t1;

return 0;
}
Result:
Test::Test()
Test::Test()
Test::~Test()
Test::~Test()

hickscorp
6th December 2006, 23:41
Well...

Unless i explicitely delete pLocalLister, i never get destructor debug from my objects.
Let me summarize how they are made:

class IFilesLister : public QObject {
Q_OBJECT
public:
IFilesLister(void);
virtual ~IFilesLister(void);
// The following method calls "DoFetchContent" which is virtual...
bool FetchContent(const CNodeDesc& cParent, QObject* pUserData=0);
protected:
virtual bool DoFetchContent(const CNodeDesc& cParent, QObject* pUserData) = 0;
};

And then:


class CFilesLister_File : public IFilesLister {
Q_OBJECT
public:
CFilesLister_File(void);
CFilesLister_File(const QString& cBasePath_p);
virtual ~CFilesLister_File(void);
protected:
virtual bool DoFetchContent(const CNodeDesc& cParent, QObject* pUserData);
};

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.