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. Code or tool for Qt memory usage/leaks
Forum Updated to NodeBB v4.3 + New Features

Code or tool for Qt memory usage/leaks

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 3 Posters 2.7k 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.
  • J Online
    J Online
    JonB
    wrote on 11 Jul 2018, 13:07 last edited by JonB 7 Nov 2018, 13:09
    #1

    I use PyQt/Python for Qt (desktop) development, under Linux. Given that, standard suggestions like valgrind for detecting memory leaks won't do me much good.

    All I'd really like to get is a list of what QObjects (probably just QWidgets) have been allocated on the heap (Python should not do any stack objects...) and which are still in existence (i.e. not deleted). With some indication of what they are ("this is a QDialog", "this has objectName() == ...). In such a form that I can search through the undoubtedly large number of ones I don't care about to drill down to suspicious ones.

    Does Qt keep a runtime, accessible list of currently existing objects it has created which I can get at from PyQt, or an external tool for this?

    K 1 Reply Last reply 11 Jul 2018, 14:48
    0
    • J Online
      J Online
      JonB
      wrote on 20 Jul 2018, 08:57 last edited by JonB
      #15

      For now I am getting what I need by walking QApplication.allWidgets() (as per @mrjj), checking for things like "no parent".

      Thanks for all your suggestions though.

      1 Reply Last reply
      1
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 11 Jul 2018, 13:10 last edited by
        #2

        HI
        Like
        http://doc.qt.io/qt-5/qapplication.html#allWidgets
        ?

        J 2 Replies Last reply 11 Jul 2018, 13:23
        1
        • M mrjj
          11 Jul 2018, 13:10

          HI
          Like
          http://doc.qt.io/qt-5/qapplication.html#allWidgets
          ?

          J Online
          J Online
          JonB
          wrote on 11 Jul 2018, 13:23 last edited by
          #3

          @mrjj
          Hmm, quite possibly...!

          So the first thing I was going to investigate is the use or non-use of QDialog.setAttribute(QtCore.Qt.WA_DeleteOnClose, True), which varies in existing code. I am unclear whether those which do not have this flag are left around persisting or not. Which would be very bad....!

          1 Reply Last reply
          0
          • M mrjj
            11 Jul 2018, 13:10

            HI
            Like
            http://doc.qt.io/qt-5/qapplication.html#allWidgets
            ?

            J Online
            J Online
            JonB
            wrote on 11 Jul 2018, 14:34 last edited by
            #4

            @mrjj
            As my app gets going, I discover len(QApplication.allWidgets()) == 896. I think I need to do some filtering before I start examining them... :)

            1 Reply Last reply
            0
            • J JonB
              11 Jul 2018, 13:07

              I use PyQt/Python for Qt (desktop) development, under Linux. Given that, standard suggestions like valgrind for detecting memory leaks won't do me much good.

              All I'd really like to get is a list of what QObjects (probably just QWidgets) have been allocated on the heap (Python should not do any stack objects...) and which are still in existence (i.e. not deleted). With some indication of what they are ("this is a QDialog", "this has objectName() == ...). In such a form that I can search through the undoubtedly large number of ones I don't care about to drill down to suspicious ones.

              Does Qt keep a runtime, accessible list of currently existing objects it has created which I can get at from PyQt, or an external tool for this?

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 11 Jul 2018, 14:48 last edited by
              #5

              Maybe Gamma ray?

              Read and abide by the Qt Code of Conduct

              M J 2 Replies Last reply 11 Jul 2018, 14:55
              0
              • K kshegunov
                11 Jul 2018, 14:48

                Maybe Gamma ray?

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 11 Jul 2018, 14:55 last edited by
                #6

                @kshegunov
                Does that work with python bindings ?
                @JonB wow, quite the amount of widgets :)

                K J 2 Replies Last reply 11 Jul 2018, 14:56
                0
                • M mrjj
                  11 Jul 2018, 14:55

                  @kshegunov
                  Does that work with python bindings ?
                  @JonB wow, quite the amount of widgets :)

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 11 Jul 2018, 14:56 last edited by
                  #7

                  @mrjj said in Code or tool for Qt memory usage/leaks:

                  Does that work with python bindings ?

                  I'm not sure, but I imagine so. In the end they are python bindings; you still get QObjects created in the background.

                  Read and abide by the Qt Code of Conduct

                  M 1 Reply Last reply 11 Jul 2018, 14:57
                  0
                  • K kshegunov
                    11 Jul 2018, 14:56

                    @mrjj said in Code or tool for Qt memory usage/leaks:

                    Does that work with python bindings ?

                    I'm not sure, but I imagine so. In the end they are python bindings; you still get QObjects created in the background.

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 11 Jul 2018, 14:57 last edited by
                    #8

                    @kshegunov
                    Yeah, i assume that also as in the end, its same binary result but i just wondered.

                    1 Reply Last reply
                    0
                    • K kshegunov
                      11 Jul 2018, 14:48

                      Maybe Gamma ray?

                      J Online
                      J Online
                      JonB
                      wrote on 11 Jul 2018, 14:59 last edited by JonB 7 Nov 2018, 15:00
                      #9

                      @kshegunov
                      Thanks. I've had a brief look. Apart from the fact that it probably does way more than I have in mind --- I only want to know what QWidgets currently exist --- I have tried to read the manual to understand. I do not install anything to find out, if I can't understand from its docs I don't bother!

                      What I don't see is what it actually "works on". The obvious approach would be attach to my running application. But, unlike you guys, my running application is always simply python3. I have no choice, that's how Python works! I don't know whether/see how it's going to recognise that is a Qt application....

                      1 Reply Last reply
                      0
                      • M mrjj
                        11 Jul 2018, 14:55

                        @kshegunov
                        Does that work with python bindings ?
                        @JonB wow, quite the amount of widgets :)

                        J Online
                        J Online
                        JonB
                        wrote on 11 Jul 2018, 15:08 last edited by JonB 7 Nov 2018, 15:09
                        #10

                        @mrjj

                        @JonB wow, quite the amount of widgets :)

                        Yes & no. This app has loads & loads of stacked widgets defined. These are effectively the many pages for the whole application (all accessed from sidebar).

                        That means they are all instantiated at start up time. All these pages' constructors are called as they are subjected to QStackedWidget.addWidget(). And the constructors create all the child widgets on the pages. It doesn't take long to reach 896 :(

                        These won't leak, and they're permanent, so I'm not worried about them. I'm more worried about the many dialogs which are created at various times, often without WA_DeleteOnClose. And figuring just how that relates to Python's reference counting and garbage collection....

                        M 1 Reply Last reply 11 Jul 2018, 15:24
                        0
                        • J JonB
                          11 Jul 2018, 15:08

                          @mrjj

                          @JonB wow, quite the amount of widgets :)

                          Yes & no. This app has loads & loads of stacked widgets defined. These are effectively the many pages for the whole application (all accessed from sidebar).

                          That means they are all instantiated at start up time. All these pages' constructors are called as they are subjected to QStackedWidget.addWidget(). And the constructors create all the child widgets on the pages. It doesn't take long to reach 896 :(

                          These won't leak, and they're permanent, so I'm not worried about them. I'm more worried about the many dialogs which are created at various times, often without WA_DeleteOnClose. And figuring just how that relates to Python's reference counting and garbage collection....

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 11 Jul 2018, 15:24 last edited by
                          #11

                          @JonB
                          Yep all those have no choice as to die with the stack widget
                          So basically, you are looking for Widgets with no parent ? .
                          As those with no WA_DeleteOnClose , would still die with parent eventually.

                          I have no idea if pythons garbage collection also includes QObjects but i suspect it would be tricky if it does.

                          J 1 Reply Last reply 11 Jul 2018, 17:07
                          0
                          • M mrjj
                            11 Jul 2018, 15:24

                            @JonB
                            Yep all those have no choice as to die with the stack widget
                            So basically, you are looking for Widgets with no parent ? .
                            As those with no WA_DeleteOnClose , would still die with parent eventually.

                            I have no idea if pythons garbage collection also includes QObjects but i suspect it would be tricky if it does.

                            J Online
                            J Online
                            JonB
                            wrote on 11 Jul 2018, 17:07 last edited by
                            #12

                            @mrjj
                            I'm looking for repeated leaks. Whether that requires "no parent" I don't know.

                            I can't recall whether you are a pythonista. All our objects are allocated on the heap, there's no stack storage. w = QWidget() is the (implicit) new QWidget() C++ statement, but there's no such thing as delete! I assume(d) Python reference-counts like C# and frees when no more references, but then someone (@SGaist I think maybe) said this wasn't all there is to it and I should consider Qt's references separately, which I'm not sure about.

                            Yes all child widgets on the QStackedWidgets will eventually die with it on GUI exit. But I only create them once. I'm more worried about the (plentiful) dialogs which come & go all the time. They are instantiated in two ways:

                            QDialog(parent = None)
                            

                            Yes, this dialog has no parent. You asked if I am interested in that? Having no parent like this is not a "leak". Does something give them a parent, perhaps when they're shown (I tend to use exec()). If not, or whether they are or not, what/when is deleting them?

                            QDialog(parent = <caller>)
                            

                            This has a parent, and the parent will likely persist (e.g. it's a stacked widget page). But again, whether or not, is this dialog being created repeatedly without being freed each time it is called?

                            I think all I really must check for is whether the total number of existing widgets is seen to keep increasing each time I go in & out of a given dialog. Which would indeed be bad for app. And I ought be able to do this using your QApplication.allWidgets().

                            M 1 Reply Last reply 11 Jul 2018, 17:25
                            0
                            • J JonB
                              11 Jul 2018, 17:07

                              @mrjj
                              I'm looking for repeated leaks. Whether that requires "no parent" I don't know.

                              I can't recall whether you are a pythonista. All our objects are allocated on the heap, there's no stack storage. w = QWidget() is the (implicit) new QWidget() C++ statement, but there's no such thing as delete! I assume(d) Python reference-counts like C# and frees when no more references, but then someone (@SGaist I think maybe) said this wasn't all there is to it and I should consider Qt's references separately, which I'm not sure about.

                              Yes all child widgets on the QStackedWidgets will eventually die with it on GUI exit. But I only create them once. I'm more worried about the (plentiful) dialogs which come & go all the time. They are instantiated in two ways:

                              QDialog(parent = None)
                              

                              Yes, this dialog has no parent. You asked if I am interested in that? Having no parent like this is not a "leak". Does something give them a parent, perhaps when they're shown (I tend to use exec()). If not, or whether they are or not, what/when is deleting them?

                              QDialog(parent = <caller>)
                              

                              This has a parent, and the parent will likely persist (e.g. it's a stacked widget page). But again, whether or not, is this dialog being created repeatedly without being freed each time it is called?

                              I think all I really must check for is whether the total number of existing widgets is seen to keep increasing each time I go in & out of a given dialog. Which would indeed be bad for app. And I ought be able to do this using your QApplication.allWidgets().

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 11 Jul 2018, 17:25 last edited by
                              #13

                              @JonB
                              Im only c++. So not sure how pythons integrates to the ownership systems
                              http://doc.qt.io/qt-5/objecttrees.html

                              The
                              QDialog(parent = None) and no WA_DeleteOnClose would leak in c++ but maybe python handles that ?
                              ( on some platform, the leak would not be true. the OS would free on app termination )

                              QDialog(parent = <caller>) would eventually be freed but would might consume memory until app exit.

                              Being python noob i was wondering if its possible to use
                              http://www.gnosis.cx/publish/programming/metaclass_2.html
                              and bind a destructor to all object so you can dump objectnames of all being deallocated.
                              But no idea if that works with bindings :)

                              J 1 Reply Last reply 11 Jul 2018, 17:38
                              0
                              • M mrjj
                                11 Jul 2018, 17:25

                                @JonB
                                Im only c++. So not sure how pythons integrates to the ownership systems
                                http://doc.qt.io/qt-5/objecttrees.html

                                The
                                QDialog(parent = None) and no WA_DeleteOnClose would leak in c++ but maybe python handles that ?
                                ( on some platform, the leak would not be true. the OS would free on app termination )

                                QDialog(parent = <caller>) would eventually be freed but would might consume memory until app exit.

                                Being python noob i was wondering if its possible to use
                                http://www.gnosis.cx/publish/programming/metaclass_2.html
                                and bind a destructor to all object so you can dump objectnames of all being deallocated.
                                But no idea if that works with bindings :)

                                J Online
                                J Online
                                JonB
                                wrote on 11 Jul 2018, 17:38 last edited by
                                #14

                                @mrjj
                                Yeah, we're both not sure how it interacts with Qt ownership :)

                                Not freeing till program exit is not in itself a problem to me. The problem would be if more than one instance of a dialog can be in existence at the same time. The point is that those QDialog() statements are being executed repeatedly: user clicks a button, dialog is created dynamically, shown and closed. One instance of that dialog. Then at a later date user clicks same button again. It will create dialog again. If previous one did not get deleted, that's a "leak" for me.

                                As I said, I think all I need --- for my simple checking --- is just the count and the types/names of the widgets from your QApplication.allWidgets(). If I see any permanent increase in that as I go in & out of various dialogs repeatedly I can investigate.

                                1 Reply Last reply
                                0
                                • J Online
                                  J Online
                                  JonB
                                  wrote on 20 Jul 2018, 08:57 last edited by JonB
                                  #15

                                  For now I am getting what I need by walking QApplication.allWidgets() (as per @mrjj), checking for things like "no parent".

                                  Thanks for all your suggestions though.

                                  1 Reply Last reply
                                  1

                                  • Login

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