Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
how to handle emit signal(dynamically allocated item that needs to be freed)
I have a dynamically allocated object which is a parameter passed to a signal with an emit signal(object); type code.
How/when can I safely delete my object.
Type *a= new Type(paramters);
@Cyrille-de-Brebisson Are you passing by reference, or as pointer?
You can let the receiving slot delete it, but if you connect more than one slot then you will be in trouble.
You should consider using smart pointers for that instead of raw pointers - smart pointer will delete the object as soon as nobody references it anymore.
At the moment, I am passing my data (variable size) by address (the data is raw allocated (malloc) ram)... but I could change things if needed.
They seem to be a VERY large number of smart pointer types in QT, any indication on which one I should use?
Since the slot/signal system seems to do a lot of (void*) cast, I am afraid that smart pointers might not work well as they might miss a copy somewhere...
VRonin last edited by VRonin
if you are 100% sure no slot will ever be executed in a secondary thread (or executed by forcing
Qt::QueuedConnection) then you can safely call
delete a;just after
hey seem to be a VERY large number of smart pointer types in QT, any indication on which one I should use?
I am afraid that smart pointers might not work well as they might miss a copy somewhere
As long as they are declared with the meta-object system you don't have to worry about anything.
QSharedPointeris automatically registered so no worries
ok, I will try it, thanks.