0

I'm trying to solve some memory leaks in my application. I noticed that when I allocate a object with the new operator (for example a QGLWidget) obviously the memory used by application increased, but when I tried to delete the object only a portion has been released. To better understand I tried a simple program like the following code:

QList<QGLWidget *> aaa;
int i;

(1)
for(i=0;i<100;i++)
    aaa.append(new QGLWidget());

(2)
for(i=0;i<100;i++)
    delete aaa.at(i); 

(3)

The application memory usage at each point is the following: (1) 4672 K (2) 413156 K (3) 111212 K

Why the deallocation isn't complete? In my original application I need to allocate and deallocate many times these objects but this situation brings a memory increase too high. I'm using Qt 4.8.4 in OpenSUSE 12.3. Can someone help me?? Thanks Thanks.

Maxim Makhun
  • 2,162
  • 1
  • 22
  • 29
  • 1
    See e.g. [this answer of mine](http://stackoverflow.com/a/23488556/440558). – Some programmer dude May 08 '14 at 15:51
  • 2
    how do you measure the memory increase? give valgrind try. – user1810087 May 08 '14 at 15:52
  • 3
    You've got a bug there. You are deleting the QGLWidget but the QList<> still has it as an item. Use `takeAt()` instead of `at()` – RobbieE May 08 '14 at 15:55
  • I understood this behavior is due to the OS optimization, but this situation is impossible for my application (it reaches more than 1 GB if I allocate/deallocate many times a QGlWidget), there's a way to avoid this bug??? – user3617113 May 08 '14 at 16:26
  • I don't think its a bug. In most software, you'll never see memory given back to the OS during runtime – Bowdzone May 08 '14 at 20:13
  • and if you reallocate the GlWidgets again what is the memory then? – ratchet freak May 09 '14 at 07:51
  • I tried, also, to put the previous piece of code into an infinite while loop, the result was that in a few seconds the memory usage reached more than 1 GB and it continued to increase. I tried with different qt objects and I noticed the same behavior. One important observation is that with standard c++ type for example std::string this increment isn't present and the memory keep costant. Is this a bug only of the qt object? Why this problem doesn't happen with the c++ standard objects? – user3617113 May 09 '14 at 10:35
  • and what if you let the code return to the event loop, deleting a QObject triggers some events that need to be handled – ratchet freak May 09 '14 at 13:53

0 Answers0