QNetworkReply* ownership
-
Hi,
I'm using an instance of QNetworkAccessManager to create GET and POST request, by using the corresponding get() and post() methods. These methods return a pointer to the QNetworkReply* that was created.
I've got several questions about this created object:
(1) Who is the owner of this QNetworkReply*? Is it the responsibility of the caller to manage the lifetime of this object? If so, does it need to be handled in the slots connected to the errorOccurred or finished signal, by calling deleteLater in these slots?
(2) Is it possible that the QNetworkReply* never emits one of the signals mentioned in (1), leading to some QNetworkReply* that will never be destroyed?Thank you very much for your answer!
-
@mistralegna said in QNetworkReply* ownership:
QNetworkReply
- Caller is responsible. It is up to you how you manage its lifetime, but usually it is done like you described it.
- You can also store the pointer as member variable or (if you have many in parallel) in a list member variable.
-
Hi,
In addition to what @jsulm wrote for number 1, since Qt 5.14, you can QNetworkAccessManager:: setAutoDeleteReplies to handle the replies lifetime.
-
Thank you very much for your answers, @jsulm and @SGaist !
In the case we use the setAutoDeleteReplies or the corresponding attribute at the QNetworkRequest level, what will happen if for some reason the finished signal is never emitted?
Does it mean that in this case, the caller still needs to ensure the QNetworkReply* will be correctly destroyed at some point?
-
AFAIK, if this does happen you likely have other bigger issues. The signal is not related to the success of the request.