For your first question, use QPointer
For your second question,
If I understood clearly, even if you are sending myObject
, you still have the reference myObject
in the class where you are emitting the signal. Then how will it be a memory leak or a dangling pointer? You can still access the myObject
from the emitted class, isn't?
Hope am clear..
Edit :
From your comments I believe you are releasing/deleting the objects in the slots. Now I assume your problem is, what if the (memory releasing) slot gets called once,twice or not called at all.
You can use QPointer for that. From the Qt documentation,
Guarded pointers (QPointer
) are useful whenever you need to store a pointer to a QObject
that is owned by someone else, and therefore might be destroyed while you still hold a reference to it. You can safely test the pointer for validity.
An example from the Qt documentation itself,
QPointer<QLabel> label = new QLabel;
label->setText("&Status:");
...
if (label)
label->show();
the explanation goes on like this..
If the QLabel is deleted in the meantime, the label variable will hold 0 instead of an invalid address, and the last line will never be executed. Here QLabel will be your MyClass
and label is your myObject
. And before using it check for Nullity.