PDA

View Full Version : Memory leak?



Enygma
4th September 2007, 10:14
Hi there.

In my program I`m using a Main Window which is a subclass of QWidget and from it, by pressing some buttons, I create new windows which are also subclasses of QWidget with parent set to 0 and which have the widget attribute Qt::WA_DeleteOnClose set.

The problem that I`m having is when monitoring the memory my application uses. Every time I open a new window inside the application, the memory usage rises but when I close it, it doesn't drop back. This repeats its self for every window I open and the memory stacks up to even 100mb VmSize at 7+1(the main window) windows opened. (VmRss also stacks up).

I`ve connected the destroyed signal for the windows and tried to see if it it gets called and it does; but what is there to be done if the memory keeps rising?

Any help? :confused:

marcel
4th September 2007, 10:26
Do you make any allocations by yourself additional to opening the windows?

Enygma
4th September 2007, 10:29
I use code like this when creating a new window:

Window *window = new Window( arg1, arg2 );
window->show();

marcel
4th September 2007, 11:09
Yes, but does the window class itself make some allocations inside?

Regards

Enygma
4th September 2007, 11:54
Of course it does.

All the gui is created in this class ( labels, groupboxes, qtrees, etc ) and some QLists and QMaps.

The widgets should get destroyed by qt when destroying the window containing them. The lists I don`t know, but I tried doing a 'delete list' for example in closeEvent and I got some degub messages and a crash saying 'double free or corruption (!prev): 0x0965a8' so I imagine they are deleted in the destructor.

marcel
4th September 2007, 12:04
The widgets won't get deleted if they are parentless.
The list also, if they are allocated dynamically and they don't have parents, they won't get deleted automatically.

So, maybe you should take a look at those debug messages. If the lists contain complex data structures, then those structures should have destructors to clean up after themselves.

Regards

Enygma
4th September 2007, 13:12
All the widgets have proper parents. Except for some QTreeWidgetItems that are added to the tree with addTopLevelitem and when the tree gets destroyed, they should too.

The lists are actually (not very efficient but nice to code at) QList<QMap<QString, QString> > * which I delete inside closeEvent. ( no more crash, the earlier rash was about me deleting a qprocess :p my bad )

From what I know, QStrings are managed by Qt.

So the question remains. Why doesn't the memory free up when each window closes? :confused:

marcel
4th September 2007, 13:19
Assuming that you are on linux, read this:http://www.qtcentre.org/forum/f-qt-programming-2/t-it-seems-that-qt-does-not-release-unused-memory-8569.html.

Although, if there aren't any leaks, the physical mem usage should drop.
vmrss is the total amount of memory used by your application, both physical and virtual.

Regards

Enygma
4th September 2007, 15:28
Yes, I use Fedora 7.

When I open the main window, the VmRss stays at ~16Mb. After opening 2 windows inside the my application, VmRss rises at ~36Mb. After I close the two windows it stays the same.
VmSize does the same, only the value is higher as expected.

I mention that the main window is an instance of the class MainWindow and the windows that open by pressing buttons are instances of the class Window. So they should get dealocated. :|

I tried the valdring tool and I do have some relatively minor leaks, most of them tracing back to qt, xerces-c and expat inside xerces which I use.

I get alot of uninitialized variables warnings and some overflows from what I understand, all of them tracing back to qt or xerces or some X11 lib. On my sources, most of them point merely to code like QApplication qapp( argc, argv ); or simple stuff like that so I can't be screwing things there. :)

I`ve read about the linux memory manager in the suggested thread, but here the used memory keeps rising.The more windows I open, the more memory it eats.

Nothing pointed out by valdrind ends up in significant parts of my code, only in the libraries that the program uses :|

I don`t understand. :(

marcel
4th September 2007, 16:11
Then it is possible to have some leaks.
But for someone to help you, it is required that you post some code.
This is only if you can. Even an example that reproduces the problem would suffice.

Regards

Enygma
4th September 2007, 16:24
Yeah, I`m fully aware of that, but like in the thread u`ve suggested, the code is very large and reducing it to a significant example would be really hard, taking in consideration also that I don't know exactly which part of it is responsable for this behaviour.

What about the claims that the libraries to which I`m linking (qt, xerces-c, x11) are leaking moderatly or using unitialized data? Am I misinterpreting valgrind's output, is it erroreous, or it is just something that does happen commonly?

Thanks for the reply troughout this thread but I think I`ll leave this behind as the conditions in which this problem could get very disturbing require a relatively disturbed user. :)