Solved How to set a pointer to an object to "nullptr" upon calling deleteLater()
-
Hi everyone,
I have a simple application where a webSkt client connects to a webSktServer. Upon client disconnection i call the deleteLater as follows:connect(pWebSkt, SIGNAL(disconnected()), this, SLOT(deleteLater());
Will deleteLater() set the pWebSkt to "nullptr". I am concerned because in other parts of my code I check the validity of this pointer before doing some functions e.g.:
if(pWebSkt) pWebSkt->doJob();
Another approch would be as follows:
connect(pWebSkt, SIGNAL(disconnected()), this, SLOT(onDisconnected()); .... void MyClass::onDisconnected() { delete pWebSkt; pWebSkt = nullptr; }
What would be the correct/recommended approach?
-
@alex_qwa
What do you need to delete the object if you assign nullptr? -
@alex_qwa said in How to set a pointer to an object to "nullptr" upon calling deleteLater():
What would be the correct/recommended approach?
QPointer<QTcpSocket> pWebSkt; // gets set to NULL upon deletion
-
@Taz742 , well i want to free the underlying resource (in this case the webSkt)
-
@alex_qwa
QPointer is the most elegant solution in my opinion. -
@raven-worx thanks for your prompt response;
Your answer solves my problem, BUT ...
1- QPointer should be used when i need a pointer to an object "OWNED" by someone else.
2- In my case I am the owner of the object (i.e. pWebSkt), so using Qpointer would incur un-required overhead. (even if it has no/little over head I would like to know how this can be achieved without using QPointer to get a better understanding on the process of how objects are destroyed and cleaned in Qt)
3- So I want to destroy/delete my resource(pWebSkt) when it gets disconnected and fires the corresponding signal. At this point I also expect to set the pointer to "nullptr".How can this be achieved? Would the following code work safely?
connect(pWebSkt, SIGNAL(disconnected()), pWebSkt, SLOT(deleteLater()); connect(pWebSkt, SIGNAL(disconnected()), this, SLOT(onDisconnected()); .... void MyClass::onDisconnected() { pWebSkt = nullptr; }
** Is the second code snippet in my original post valid (i.e. deleting the pointer in the slot and then setting it to nullptr)?
Thanks in advance
-
- You are effectively delegating
pWebSkt
to delete itself so you can lose ownership (as long as pWebSkt has also a parent) - no overhead really, QPointer is basically a slot connected to the
destroyed
signal and nothing more - your example works, you can, alternatively connect to
QObject::destroyed
but that's exactly what QPointer does
- You are effectively delegating
-
@VRonin I am new to Qt, so sorrry if my questions are no good.
if i've understood correctly you are telling me that I can setvoid MyClass::onDestroyed() { pWebSkt=nullptr }
Q: Then how can the object (which is set to nullptr) destroy its childs immediately after this signal is emmited as suggested by the documentation of "destroyed" signal
-
@alex_qwa said in How to set a pointer to an object to "nullptr" upon calling deleteLater():
Q: Then how can the object (which is set to nullptr) destroy its childs immediately after this signal is emmited as suggested by the documentation of "destroyed" signal
you just set your local pointer (which holds the address to memory) to null. The object still exists in the memory though and gets deleted once the control returns from your onDestroyed() slot.
So the pointer does, as the name says, only point to a memory address, but is not the object itself per se.
Hope its clearer now. -
@raven-worx thanks a lot