[Interest] Memory Leak when instantiating QWidget based windows

Bo Thorsen bo at vikingsoft.eu
Fri Sep 9 13:15:53 CEST 2016


Den 08-09-2016 kl. 12:55 skrev Konstantin Shegunov:
>
> On Wed, Sep 7, 2016 at 11:23 PM, Mike Jackson <imikejackson at gmail.com
> <mailto:imikejackson at gmail.com>> wrote:
>
>     We monitor the memory use before and after the loop using OS X's
>     Activity monitor. At the end of the loop there is more memory being
>     used than before the loop, by about 2~3MB worth.
>
> This isn't reliable. Consider the following C++ only code:
>
> int main(intargc,char**argv)
> {
>   struct X;
>   X * x = nullptr;
>
>   struct X {
>     int data[1024];
>   };
>
>   for(qint32i=0;i<100000;i++){
>     x = new X[1024];
>     delete x;
>   }
>
>   return 0;
> }
>
> In the KSysGuard (KDE's system monitor) I observe for the memory
> consumption: 12 908k, 23 048k shared before the loop and 12 944k, 25
> 112k shared after the loop. There isn't a leak here, yet there's a
> difference. The OS's heap manager may free the memory immediately, may
> cache it, or hold on to it. Ultimately it's out of your control, that's
> why you should use a code analyzer (like valgrind to track leaks).

You need to use "delete[] x;" or you can't say that you don't have a 
memory leak.

Bo Thorsen,
Director, Viking Software.

-- 
Viking Software
Qt and C++ developers for hire
http://www.vikingsoft.eu



More information about the Interest mailing list