Events during form close…
Do signals on the main application finish before the application ~mainForm() happens when exiting the application?
I know you usually want to wait for other threads to finish before prematurely destroying the active thread, but if you have a signal from another thread hitting the main application, is there a way to wait for those signals to finish before the main thread closes?
I am getting an odd exception on my form close…that’s the only reason why I am asking.
Signals from other threads are not delivered directly, but via Qt’s event system. So it may be that the object has been destroyed already. Instead of calling deleted directly on the object (delete xyz) you could schedule the destruction using Qt’s event system by calling xyz->deleteLater(). It’s just a guess, I did not test it.
Instead of calling deleted directly on the object (delete xyz) you could schedule the destruction using Qt’s event system by calling xyz->deleteLater(). It’s just a guess, I did not test it.
This might solve the problem, but it is not guaranteed. If an object from another thread issues an async signal after deleteLater call, it will be put to the event queue after the delete call. So same issue.
You can try to catch the closing action checking
- QCoreApplication::closingDown ()
If the application is closing, the value returns true.
Another way is capturing the QEvent::close() on your QCoreApplication.
At application closeDown the form is most probably already destroyed.
Using deleteLater() makes sure that the object is only deleted at the next run of the event loop – this usually happens when the slots directly connected to a signal have finished their work (unless you call processEvents manually). Every pending event directed to the object to be deleted is nuked in that case.