Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

life time of object associated with signal

Tags:

c++

qt

Here is my sample qt connect statement

connect(pHttpFetch, SIGNAL(Fetched(QByteArray)), this, SLOT(PrintData(QByteArray)));

Here the signal of first object is connected to the slot of the invoking(which makes the connect call) object.

I have the following things

  • The first object is a local object. The object is killed when control goes out of scope.
  • The invoking object will stay in memory throughout the application memory.

As I don't need the first object, is it fine to make it a local object ? ( I assume Qt magically keeps the object in memory)

Should I make a shared pointer to hold the object. Will destroy the object when not required ?

like image 688
asitdhal Avatar asked Oct 15 '25 08:10

asitdhal


1 Answers

According to the Qt documentation

All signals to and from the object are automatically disconnected, and any pending posted events for the object are removed from the event queue.

And no, Qt doesn't "magically" keep the object in memory.

An object that doesn't exist anymore can't send signals. You should allocate memory for this object and keep a reference to it. Remember that if you gave your QObject a parent, then this parent will automatically handle the deletion of their child (but if you don't provide a parent, you'll have to delete it manually or use the deleteLater() slot of QObject)

like image 99
JBL Avatar answered Oct 17 '25 20:10

JBL