Is it safe to connect a QObject's deleteLater to one of its own signals?
-
By this I mean if I were to connect an object's
deleteLater()
to one of its signals, when I emit the signal, is it guaranteed that it will reach all other slots connected to it for the same object? I don't fully grasp the internal workings of the signals/slots mechanism, and I can't confirm this for myself. -
deleteLater will be called in the order of the signal handlers were attached. However, it won't actually delete well after all the other signals have been handled.
QObject obj1; QObject ob2; obj1.connect(&obj1, &QObject::somesig, &obj1, &QObject::deleteLater); obj1.connect(&obj1, &QObject::somesig, &obj2, &QObject::otherslot);
This will call deleteLater before otherslot. But the delete won't happen until after these signals have been handled. It won't be until the next iteration of the event loop.
It is common to use deleteLater with network response objects in addition to handlers on the finished (?) signal. This will cause the object to clean itself up, but the handler will also have an opportunity to "handle" the data.