Force garbage collection in Qt

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

  • Qt Champions 2017


    Do you mean the parent/child system and widgets?

    Or what garbage collection do you mean ?

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

  • Qt Champions 2017

    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.

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

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

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

  • Qt Champions 2017

    as notes

    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.

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

  • Qt Champions 2017

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

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

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

  • Qt Champions 2017

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

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

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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.