[Interest] Reasons why deleteLater() might not work

Sérgio Martins sergio.martins at kdab.com
Fri Apr 1 17:22:20 CEST 2022

On 2022-04-01 14:17, Michael Jackson wrote:
> I have a bit of code where in I am removing a QWidget from the UI and
> it needs to be truly cleaned up as its parent QObject is also being
> cleaned up. I thought I did the appropriate:
> layout->removeWidget(widget);
> widget->setParent(nullptr);
> widget->deleteLater();
> I put a "std::cout <<... " in the widget's destructor but I never see
> it printed. I put a break point in the destructor and the break point
> never gets hit. Looked around on the internet, various forums, double
> checked that the event loop is actually running, called
> qtapp->eventLoop()->processEvents() (or whatever that bit of code is)
> a few times. Nothing. Currently we are leaking memory because we are
> creating the widget but never deleting the widget when it is removed
> from the UI. I'll assume since I have complete control of the UI that
> the event loop is running. The last line in our main() is app.exec();
> so I am assuming the event loop is running. This was originally with a
> self-built Qt 5.15.2 on macOS 10.15 (Catalina) but I installed the
> normal Qt 5.15.2 from www.qt.io and it behaves the same way. I have
> other applications where this does actually work so I am trying to
> figure out where in this new version of the application we messed up.
> If anyone has any thoughts on where to look I would really appreciate
> the sanity check.
> If we are in the middle of a signal being fired would that stop the
> deleteLater() from working?
> Do I need to manually disconnect all signals/slots for deleteLater() to 
> work?
> We have a mix of new style connects and old style connects? Is that
> messing with deleteLater()?

One situation I've came across in the past was:

Sérgio Martins | sergio.martins at kdab.com | Senior Software Engineer
Klarälvdalens Datakonsult AB, a KDAB Group company
Tel: Sweden (HQ) +46-563-540090, USA +1-866-777-KDAB(5322)
KDAB - The Qt, C++ and OpenGL Experts

More information about the Interest mailing list