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. Force garbage collection in Qt
Forum Updated to NodeBB v4.3 + New Features

Force garbage collection in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 5 Posters 7.9k Views 2 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.
  • G Offline
    G Offline
    gchauvie
    wrote on last edited by
    #1

    I have some bugs appearing in my application because I keep pointers to objects that are deleted by the garbage collection of Qt. I know this is bad but I would like to know if there is a way to make garbage collection appear more often so I can more easily find out about these problems.

    mrjjM J.HilkJ 2 Replies Last reply
    0
    • G gchauvie

      I have some bugs appearing in my application because I keep pointers to objects that are deleted by the garbage collection of Qt. I know this is bad but I would like to know if there is a way to make garbage collection appear more often so I can more easily find out about these problems.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @gchauvie

      Hi
      Do you mean the parent/child system and widgets?

      Or what garbage collection do you mean ?

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gchauvie
        wrote on last edited by
        #3

        Yes exactly. I open a panel and the close it. It will be destroyed sooner or later but I cannot control exactly the moment when the panel will be destroyed. I would like it to happen quickly so I can see my problems appear soon.

        mrjjM 1 Reply Last reply
        0
        • G gchauvie

          Yes exactly. I open a panel and the close it. It will be destroyed sooner or later but I cannot control exactly the moment when the panel will be destroyed. I would like it to happen quickly so I can see my problems appear soon.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @gchauvie
          you can just say
          delete panel;
          and it will be deleted. ( dont do that ON childs of something)
          Close is not the same as delete.
          Do you mean when using DeleteLater flag ?

          You can make a new window ( a test window)
          and construct a instance of the window you want to test.
          Then delete it.
          It should be freed very shortly after.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            gchauvie
            wrote on last edited by
            #5

            Yes but I would like it a general solution for all the similar problems that could appear in my application because if I have to open a panel, close the panel. Wait for 15 seconds and then do something else to make the app crash, I won't find new problems as easily as if the wait time is just 1 second.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kenchan
              wrote on last edited by
              #6

              I guess you mean stuff that has been deleted by Qt with the deleteLater() function?
              The closest you might get to forcing a garbage collection like thing is to use the sendPostedEvents(Q_NULLPTR,QEvent::DeferredDelete) of your application at an appropriate time.
              It might be better to delete the objects yourself and set their pointers to NULL after deleting them so you can check for that later.

              1 Reply Last reply
              1
              • G Offline
                G Offline
                gchauvie
                wrote on last edited by
                #7

                Thanks kenchan, I'll try it and keep you posted. It looks like it is what I was looking for.

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi
                  as notes
                  http://doc.qt.io/qt-5/qobject.html#deleteLater

                  Also if you think your issue is that you are using a widget after it has been deleted then you can use this signal
                  void QObject::destroyed(QObject *obj = Q_NULLPTR)
                  It will then tell you when dying and u can clear pointer or what the real issue is.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    gchauvie
                    wrote on last edited by
                    #9

                    I tried sendPostedEvents(Q_NULLPTR,QEvent::DeferredDelete) but it does not seem to make my problems appear more often. I still have to wait at least 30 seconds between the moment I close my panel and the moment the destructors are called.

                    I know how to correct it but I would just like it to fail fast.

                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Hi
                      if you have many loops/strangulate the event loop, it might take a while before its actually deleted.

                      1 Reply Last reply
                      0
                      • G gchauvie

                        I have some bugs appearing in my application because I keep pointers to objects that are deleted by the garbage collection of Qt. I know this is bad but I would like to know if there is a way to make garbage collection appear more often so I can more easily find out about these problems.

                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #11

                        @gchauvie
                        If you make your UI with QML, than you can use the global object gc() to manualy trigger the garbage collector.

                        But thats a QML feature only, I think.


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        G 1 Reply Last reply
                        2
                        • VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #12

                          Given we are talking about QObjects, you can use QPointer intead of a raw pointer. It becomes null when the object gets destroyed

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          mrjjM 1 Reply Last reply
                          3
                          • VRoninV VRonin

                            Given we are talking about QObjects, you can use QPointer intead of a raw pointer. It becomes null when the object gets destroyed

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @VRonin
                            Ofc, that much easier than using QObject::destroyed :)
                            More coffee for me...

                            1 Reply Last reply
                            0
                            • K Offline
                              K Offline
                              kenchan
                              wrote on last edited by
                              #14

                              @gchauvie you can replace the Q_NULLPTR argument with a pointer to specific widgets. As I said it all about the appropriate time when you use that function.

                              Directly deleting the objects with delete is the fasted way but you must write your program to manage the pointers you will delete by initialising them to NULL resetting them to NULL after delete and checking for null before you reallocate etc.... you must be very careful which objects you delete when you do it this way.

                              1 Reply Last reply
                              0
                              • J.HilkJ J.Hilk

                                @gchauvie
                                If you make your UI with QML, than you can use the global object gc() to manualy trigger the garbage collector.

                                But thats a QML feature only, I think.

                                G Offline
                                G Offline
                                gchauvie
                                wrote on last edited by
                                #15

                                @J.Hilk This is exactly what I wanted. I should have said that I use qml earlier. Thanks a lot for your help.

                                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