Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Another newbie question - memory leaks in QApp->exec()

Another newbie question - memory leaks in QApp->exec()

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 645 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fbs7
    wrote on last edited by Fbs7
    #1

    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!!

    Christian EhrlicherC D 2 Replies Last reply
    0
    • F Fbs7

      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!!

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      F 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        @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.

        F Offline
        F Offline
        Fbs7
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I would recommend a memory leak tool like valgrind, AdressSanitizer or others which supports correct analysis and good suppression options.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • F Fbs7

            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!!

            D Offline
            D Offline
            DerReisende
            wrote on last edited by
            #5

            @Fbs7 If you are using MSVC 2019 then you should be able to simply use the /fsanitize=address compiler switch with debug mode to see mem leaks. Works quite nicely as far as I have used it. You can find more info about it here.

            1 Reply Last reply
            2
            • F Offline
              F Offline
              Fbs7
              wrote on last edited by
              #6

              Excellent - thanks both!

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved