PDA

View Full Version : deleteLater?



ggdev001
4th March 2013, 12:39
Hello,

I have a class CTest which has many instances of some class called CNetworkManager.
e.g., many functions in CTest create CNetworkManager objects. e.g.,

void CTest:f1()
{
CNetworkManager *p = new CNetworkManager();
//...do some stuff
// e.g., makePostRequest

}
void CTest:f2()
{
CNetworkManager *p = new CNetworkManager();
//...do some other stuff
//e.g., makePostRequest

}
CNetworkManager is used for network communication. Inside it uses a QNetworkAccessManager
object for that. I usually process a server response in the requestFinished of the CNetworkManager
class and call a CTest method (as callback -- and also pass the data from the server).

You can see I may have many instances of CNetworkManager in my app. My question is where
is it safe to delete these CNetworkManager objects ???? and how?

Thank you.

wysota
4th March 2013, 12:50
You can delete them once you're done using them either by an explicit delete call or with deleteLater().

ggdev001
4th March 2013, 12:57
You can delete them once you're done using them either by an explicit delete call or with deleteLater().

Hi, thanks for your response.
In the requestFinished of the CNetworkManager class I invoke a callback from my CTest as I mentioned, e.g., like this:



void CNetworkManager::requestFinished(QNetworkReply* reply)
{
//...
this->ptCallBackCTest(ptObject, netResponse);
// (*) we will get here - to line number 5 - after the callback finished execution right? So, if I delete here the CNetworkManager instance or "this" in other
// words it means I am "killing myself" within my own function.
//..
}

In other way, I could also pass a pointer to "this" (above code) to the callback function and try to delete it inside the CTest callback -- but in that
case I will never reach the 5th line as above -- and will not it cause crash??? Thanks.

wysota
4th March 2013, 13:52
I don't understand why you'd want to kill the manager when a request finishes. How do you know that no more requests are coming?

ggdev001
4th March 2013, 13:59
How do you know that no more requests are coming?

Because for each request I create a new CNetworkManager object (maybe this is not recommended but that is how it is now). So each one usually handles one request.


I don't understand why you'd want to kill the manager when a request finishes
I don't mind. I am just not sure if it is ok. As it can be seen from my implementation the requestFinished is implemented as a member function of CNetworkManager:


void CNetworkManager::requestFinished(QNetworkReply* reply)
{
...
// Is it ok to call: "delete this" here??
// now we are in the instance created at line 3 of my initial Code snippet.
}

So I was not sure if it is ok to call delete "this" as above? (e.g., in a member function). is it? Thanks.

wysota
4th March 2013, 15:11
Because for each request I create a new CNetworkManager object (maybe this is not recommended but that is how it is now). So each one usually handles one request.
That's pretty... well... unwise... Anyway, if that's the case, you can call deleteLater() in requestFinished. I wouldn't call plain delete though, it'd be very error prone (think what happens if someone else connects to the same signal and tries to access the manager).

ggdev001
4th March 2013, 15:39
you can call deleteLater() in requestFinished

You mean like: this->deleteLater() ??


think what happens if someone else connects to the same signal
this should not happen. As I mentioned each CNetworkManager object separately issues only one POST request and waits for that response.

wysota
4th March 2013, 15:52
You mean like: this->deleteLater() ??
Yes. Or simply "deleteLater()".


this should not happen.
Well... "should not" and "will not" are two different things. Nothing will prevent your collegue (or yourself in two years from now) to write a connect statement that will connect to that signal.

d_stranz
4th March 2013, 21:01
Excuse me for jumping in, but is this a typical use case for deleteLater()? That is, a QObject instance must live long enough for it to be used in a slot or other method, at which point it can be deleted, and that deletion occurs the next time the event loop in which deleteLater() was called is entered?

What happens in the case where the signal that passes the instance pointer is handled by more than one slot? Does moving from one slot to the next count as a "return to the event loop"? Or is the invocation of all the connected slots happen within the scope of a single event, and only after all slots have been called is the instance deleted?

If the latter is the case, this is a pretty useful scenario for when stack-based instances won't work.

wysota
4th March 2013, 21:58
Does moving from one slot to the next count as a "return to the event loop"?
No, it doesn't.