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