[Interest] Reasons why deleteLater() might not work

Allan Sandfeld Jensen kde at carewolf.com
Fri Apr 1 16:19:38 CEST 2022


On Freitag, 1. April 2022 15:17:59 CEST 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()? --
> Mike Jackson
> 
> 
You have to make sure the event loop is running on the thread owning the 
QObject. Do you have more than one thread?

And remember that std::cout needs a std::endl to flush and print anything. I 
normally use qDebug() or qWarning() instead as a safer logging tool, since I 
often forget the std::endl myself.

Best regards
Allan




More information about the Interest mailing list