Another newbie question - memory leaks in QApp->exec()
-
Another basic question that I'm struggling with for half a day already - sorry if it's an obvious question. If I run this in Visual Studio 2019 latest release:
int APIENTRY WinMain(...etc...) { QApplication app(__argc, __argv); _CrtMemState startupMainMenu; _CrtMemCheckpoint(&startupMainMenu); int* pi = new int(7); _CrtMemDumpAllObjectsSince(&startupMainMenu); return 0 }... then that will print the memory allocations since the checkpoint; it will correctly print my number 7 left behind in the heap (ie, a memory leak), and nothing else... very nice:
E:\Dev\MyApp\src\Main.cpp(18) : {2777} normal block at 0x0000018A4567BB30, 4 bytes long. Data: < > 07 00 00 00 Object dump complete.But if I run this code:
int APIENTRY WinMain(...etc...) { _CrtMemState startupMainMenu; _CrtMemCheckpoint(&startupMainMenu); QApplication * papp = new QApplication(__argc, __argv); QWidget* pmain = new QWidget(); pmain->show(); int nreturn = papp->exec(); delete pmain; delete papp; int* pi = new int(7); _CrtMemDumpAllObjectsSince(&startupMainMenu); return nreturn; }I expected that my deletes would have cleaned up the Qt objects, and again I'd have my number 7 as the only memory leak. Instead I have a lot of Qt-related objects left behind in the heap (286 objects, to be precise).
This is messing up my leak-detection framework, as the ton of Qt objects left in the heap by QApp->exec() makes it difficult for me to identify what is truly my memory leak and what is Qt memory leak.
I guess I could filter out by the annotated allocs ("E:\Dev\MyApp\src..."), as the Qt allocs will not have filename annotations, but, c'mon, it's not exactly good practice to leave a lot of stuff hanging in the heap.
So, is there a way to clean up all the random stuff that QApp->exec() creates in the heap? Like a QApp->clearStaticObjects() or whatever?
Thanks in advance!!
-
Another basic question that I'm struggling with for half a day already - sorry if it's an obvious question. If I run this in Visual Studio 2019 latest release:
int APIENTRY WinMain(...etc...) { QApplication app(__argc, __argv); _CrtMemState startupMainMenu; _CrtMemCheckpoint(&startupMainMenu); int* pi = new int(7); _CrtMemDumpAllObjectsSince(&startupMainMenu); return 0 }... then that will print the memory allocations since the checkpoint; it will correctly print my number 7 left behind in the heap (ie, a memory leak), and nothing else... very nice:
E:\Dev\MyApp\src\Main.cpp(18) : {2777} normal block at 0x0000018A4567BB30, 4 bytes long. Data: < > 07 00 00 00 Object dump complete.But if I run this code:
int APIENTRY WinMain(...etc...) { _CrtMemState startupMainMenu; _CrtMemCheckpoint(&startupMainMenu); QApplication * papp = new QApplication(__argc, __argv); QWidget* pmain = new QWidget(); pmain->show(); int nreturn = papp->exec(); delete pmain; delete papp; int* pi = new int(7); _CrtMemDumpAllObjectsSince(&startupMainMenu); return nreturn; }I expected that my deletes would have cleaned up the Qt objects, and again I'd have my number 7 as the only memory leak. Instead I have a lot of Qt-related objects left behind in the heap (286 objects, to be precise).
This is messing up my leak-detection framework, as the ton of Qt objects left in the heap by QApp->exec() makes it difficult for me to identify what is truly my memory leak and what is Qt memory leak.
I guess I could filter out by the annotated allocs ("E:\Dev\MyApp\src..."), as the Qt allocs will not have filename annotations, but, c'mon, it's not exactly good practice to leave a lot of stuff hanging in the heap.
So, is there a way to clean up all the random stuff that QApp->exec() creates in the heap? Like a QApp->clearStaticObjects() or whatever?
Thanks in advance!!
@Fbs7 said in Another newbie question - memory leaks in QApp->exec():
So, is there a way to clean up all the random stuff that QApp->exec() creates in the heap? Like a QApp->clearStaticObjects() or whatever?
No it's not - the stuff your see is no real leak - it's only allocated once during the complete runtime of the application and not cleaned up on exit. Feel free to provide patches to fix them.
-
@Fbs7 said in Another newbie question - memory leaks in QApp->exec():
So, is there a way to clean up all the random stuff that QApp->exec() creates in the heap? Like a QApp->clearStaticObjects() or whatever?
No it's not - the stuff your see is no real leak - it's only allocated once during the complete runtime of the application and not cleaned up on exit. Feel free to provide patches to fix them.
@Christian-Ehrlicher thank you for the answer - that's why I used the expression "stuff left behind in the heap by QApp", instead of calling them QApp memory leaks.
I guess you're advising that's, at this moment, the expected behavior of QApp. That helps me, as now I know I'm not making a mistake with some setting or missed cleaning call. I thank you for that.
But, that stuff does complicate detection of real memory leaks. So, as an extension of the question, is there any recommendation on how to detect memory leaks on Qt-based applications, given the expected behavior from QApp is to leave new objects hanging on the heap? My original thought was to take memory checkpoints, and then compare; maybe I need to use a different approach.
-
I would recommend a memory leak tool like valgrind, AdressSanitizer or others which supports correct analysis and good suppression options.
-
Another basic question that I'm struggling with for half a day already - sorry if it's an obvious question. If I run this in Visual Studio 2019 latest release:
int APIENTRY WinMain(...etc...) { QApplication app(__argc, __argv); _CrtMemState startupMainMenu; _CrtMemCheckpoint(&startupMainMenu); int* pi = new int(7); _CrtMemDumpAllObjectsSince(&startupMainMenu); return 0 }... then that will print the memory allocations since the checkpoint; it will correctly print my number 7 left behind in the heap (ie, a memory leak), and nothing else... very nice:
E:\Dev\MyApp\src\Main.cpp(18) : {2777} normal block at 0x0000018A4567BB30, 4 bytes long. Data: < > 07 00 00 00 Object dump complete.But if I run this code:
int APIENTRY WinMain(...etc...) { _CrtMemState startupMainMenu; _CrtMemCheckpoint(&startupMainMenu); QApplication * papp = new QApplication(__argc, __argv); QWidget* pmain = new QWidget(); pmain->show(); int nreturn = papp->exec(); delete pmain; delete papp; int* pi = new int(7); _CrtMemDumpAllObjectsSince(&startupMainMenu); return nreturn; }I expected that my deletes would have cleaned up the Qt objects, and again I'd have my number 7 as the only memory leak. Instead I have a lot of Qt-related objects left behind in the heap (286 objects, to be precise).
This is messing up my leak-detection framework, as the ton of Qt objects left in the heap by QApp->exec() makes it difficult for me to identify what is truly my memory leak and what is Qt memory leak.
I guess I could filter out by the annotated allocs ("E:\Dev\MyApp\src..."), as the Qt allocs will not have filename annotations, but, c'mon, it's not exactly good practice to leave a lot of stuff hanging in the heap.
So, is there a way to clean up all the random stuff that QApp->exec() creates in the heap? Like a QApp->clearStaticObjects() or whatever?
Thanks in advance!!