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. Freeing up memory for global variables
Forum Updated to NodeBB v4.3 + New Features

Freeing up memory for global variables

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 7 Posters 3.6k Views 3 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.
  • JonBJ JonB

    @mpergand said in Freeing up memory for global variables:

    If you have only one window, your app will quit as soon as this window is closed (default behavior of QApplication)

    Untested, but doesn't QWidget *widget = new QWidget; return app.exec() leak the newed widget/window (e.g. for valgrind) if you don't delete or set auto-delete on close on it?

    M Offline
    M Offline
    mpergand
    wrote on last edited by
    #11

    @JonB
    If the app process no longer exists, I really don't know what could leak.

    JonBJ 1 Reply Last reply
    1
    • M mpergand

      @JonB
      If the app process no longer exists, I really don't know what could leak.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #12

      @mpergand Do you not use a memory analyzer such as valgrind?

      1 Reply Last reply
      0
      • JonBJ JonB

        @mpergand said in Freeing up memory for global variables:

        If you have only one window, your app will quit as soon as this window is closed (default behavior of QApplication)

        Untested, but doesn't QWidget *widget = new QWidget; return app.exec() leak the newed widget/window (e.g. for valgrind) if you don't delete or set auto-delete on close on it?

        S Online
        S Online
        SimonSchroeder
        wrote on last edited by
        #13

        @JonB said in Freeing up memory for global variables:

        Untested, but doesn't QWidget *widget = new QWidget; return app.exec() leak the newed widget/window (e.g. for valgrind) if you don't delete or set auto-delete on close on it?

        If nobody deletes it, it is leaked. I would hope that valgrind would catch it (haven't used valgrind on real projects, yet). However, it is really annoying if you close an application and it takes a couple of seconds to actually disappear because it is doing all kinds of clean up. The operating system will reclaim all memory anyway. So, I'm in the camp of "don't clean up after yourself" when closing your application. Not as a hard rule, but certainly as a place for optimization of the user experience.

        JonBJ 1 Reply Last reply
        0
        • D DmitryTS has marked this topic as solved on
        • JonBJ JonB

          @mpergand said in Freeing up memory for global variables:

          If you have only one window, your app will quit as soon as this window is closed (default behavior of QApplication)

          Untested, but doesn't QWidget *widget = new QWidget; return app.exec() leak the newed widget/window (e.g. for valgrind) if you don't delete or set auto-delete on close on it?

          D Offline
          D Offline
          DmitryTS
          wrote on last edited by
          #14

          @JonB I just read about QScopedPointer and there was such an example, do I understand correctly that the QWidget will a priori be created through smart pointers and therefore there will be no problems with clearing memory?!IMG_6303.png

          SGaistS 1 Reply Last reply
          0
          • D DmitryTS

            @JonB I just read about QScopedPointer and there was such an example, do I understand correctly that the QWidget will a priori be created through smart pointers and therefore there will be no problems with clearing memory?!IMG_6303.png

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #15

            @DmitryTS None of these pairs of lines are equivalent. A QScopedPointer deletes the object it points to when it is destroyed.

            A raw pointer that is destroyed because it gets out of scope does not trigger the deletion of the objects it points to. You have to explicitly use delete on it before.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            D 1 Reply Last reply
            2
            • S SimonSchroeder

              @JonB said in Freeing up memory for global variables:

              Untested, but doesn't QWidget *widget = new QWidget; return app.exec() leak the newed widget/window (e.g. for valgrind) if you don't delete or set auto-delete on close on it?

              If nobody deletes it, it is leaked. I would hope that valgrind would catch it (haven't used valgrind on real projects, yet). However, it is really annoying if you close an application and it takes a couple of seconds to actually disappear because it is doing all kinds of clean up. The operating system will reclaim all memory anyway. So, I'm in the camp of "don't clean up after yourself" when closing your application. Not as a hard rule, but certainly as a place for optimization of the user experience.

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #16

              @SimonSchroeder , @mpergand
              And I am not of the camp who say "don't bother to delete when you are exiting because everything gets destroyed anyway". Thereby hangs the indistinguishability of what are "genuine" leaks in your code versus ones which you choose not to count as leaks because they are "top-level" or "you know about them". It seems to me there are two possible situations:

              • There is only "one" top-level/global allocated variable: in which case it's one line of code to free it and it won't take any time.
              • There are "hundreds" of such, or in some "hierarchy": in which case I revert to if you don't free them you won't be able to see anything left over which are your "genuine" leaks.

              We are, of course, all entitled to our styles/opinions. I really don't see how you use valgrind or equivalent properly with your approach. If you really feel it could take too long in production then at least write the disposal code in a #ifdef or run-time conditional check which you check during development/analysis and disable for end-user.

              @DmitryTS
              So there you are: you have different opinions. As @SGaist says your examples of QScopedPointer are not the same as the non-ones. You can use it if you wish, but my own feeling is while you are learning it does little harm to write explicit code to do the deleting.

              In itself this has little to do with why you want "global variables" in the very first place.

              Christian EhrlicherC D 2 Replies Last reply
              1
              • SGaistS SGaist

                @DmitryTS None of these pairs of lines are equivalent. A QScopedPointer deletes the object it points to when it is destroyed.

                A raw pointer that is destroyed because it gets out of scope does not trigger the deletion of the objects it points to. You have to explicitly use delete on it before.

                D Offline
                D Offline
                DmitryTS
                wrote on last edited by
                #17

                @SGaist Did I understand correctly, that the QWidget object will not be deleted, if there is the following example, and I will first need to explicitly delete it using delete?
                int main()
                {
                QApplication a(…);
                QWidget *myWidget = new QWidget;
                QScopedPointer <myWidget> pSmart;
                return a.exec();
                }

                jsulmJ 1 Reply Last reply
                0
                • JonBJ JonB

                  @SimonSchroeder , @mpergand
                  And I am not of the camp who say "don't bother to delete when you are exiting because everything gets destroyed anyway". Thereby hangs the indistinguishability of what are "genuine" leaks in your code versus ones which you choose not to count as leaks because they are "top-level" or "you know about them". It seems to me there are two possible situations:

                  • There is only "one" top-level/global allocated variable: in which case it's one line of code to free it and it won't take any time.
                  • There are "hundreds" of such, or in some "hierarchy": in which case I revert to if you don't free them you won't be able to see anything left over which are your "genuine" leaks.

                  We are, of course, all entitled to our styles/opinions. I really don't see how you use valgrind or equivalent properly with your approach. If you really feel it could take too long in production then at least write the disposal code in a #ifdef or run-time conditional check which you check during development/analysis and disable for end-user.

                  @DmitryTS
                  So there you are: you have different opinions. As @SGaist says your examples of QScopedPointer are not the same as the non-ones. You can use it if you wish, but my own feeling is while you are learning it does little harm to write explicit code to do the deleting.

                  In itself this has little to do with why you want "global variables" in the very first place.

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

                  @JonB I fully agree - not cleaning stuff up on exit will only harm memory leak debugging and may also lead to crashes now and then on exit due to wrong order of deletion.

                  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
                  1
                  • D DmitryTS

                    @SGaist Did I understand correctly, that the QWidget object will not be deleted, if there is the following example, and I will first need to explicitly delete it using delete?
                    int main()
                    {
                    QApplication a(…);
                    QWidget *myWidget = new QWidget;
                    QScopedPointer <myWidget> pSmart;
                    return a.exec();
                    }

                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #19

                    @DmitryTS said in Freeing up memory for global variables:

                    Did I understand correctly

                    No. You misunderstood what @SGaist wrote.
                    In that code snippet the QWidget will be deleted because of QScopedPointer.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @SimonSchroeder , @mpergand
                      And I am not of the camp who say "don't bother to delete when you are exiting because everything gets destroyed anyway". Thereby hangs the indistinguishability of what are "genuine" leaks in your code versus ones which you choose not to count as leaks because they are "top-level" or "you know about them". It seems to me there are two possible situations:

                      • There is only "one" top-level/global allocated variable: in which case it's one line of code to free it and it won't take any time.
                      • There are "hundreds" of such, or in some "hierarchy": in which case I revert to if you don't free them you won't be able to see anything left over which are your "genuine" leaks.

                      We are, of course, all entitled to our styles/opinions. I really don't see how you use valgrind or equivalent properly with your approach. If you really feel it could take too long in production then at least write the disposal code in a #ifdef or run-time conditional check which you check during development/analysis and disable for end-user.

                      @DmitryTS
                      So there you are: you have different opinions. As @SGaist says your examples of QScopedPointer are not the same as the non-ones. You can use it if you wish, but my own feeling is while you are learning it does little harm to write explicit code to do the deleting.

                      In itself this has little to do with why you want "global variables" in the very first place.

                      D Offline
                      D Offline
                      DmitryTS
                      wrote on last edited by
                      #20

                      @JonB thanks, global var of course it is used , I just took a micro piece, I just want to figure out how they can be cleared, since I cannot change these global variables, they are used in 10,000 lines, and most of all they are used for qml components( calling some additional functions),
                      in principle, all the work is done on a very old version of qt, and the launch comes from a docker container, where there is not even valgrind :(

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

                        A global scoped pointer to a QObject will be destroyed after QCoreApplication is gone so it will likely crash. Fix your code and don't use global variables.

                        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
                        3

                        • Login

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