I think that there is a problem with deleting QNetworkReply in case of continious communication between the client and server. I have a QEvenLoop-based class that initiates connection to the server, processes reply and repeates this cycle indefinitely.
In "isFinished" slot connected to corresponding QNetworkAccessManager signal I am calling QNetworkReply->deleteLater(). After 5-10 cycles I am getting an error. Review of the stack indicates that even though isFinished is called by accessmanager, it still wants to write something in networkReply instance later on when the instance is already deleted.
My workaround is: instead of calling "deleteLater" in "isFinished" slot I store instance of QNetworkReply in the smart pointer. Therefore QNetworReply is released not in the current cycle of communication, but in "next" cycle. My class design looks like this:
{
signals:
void sendSomeDataToServer();
slot:
void onSendSomeDataToServer();
...
boost::shared_ptr<QNetworkReply> _delayedRelease;
QNetworkAccessManager _http;
}
myClass::myClass()
{
...
connect(this, sendSomeDataToServer, this, OnSendSomeDataToServer, Qt::ConnectionType::Queued);
}
myClass::run()
{
OnsendSomeDataToServer();
exec();
}
myclass::OnsendSomeDataToServer()
{
...
_http::post();
}
myclass::isFinished(QNetworReply* reply)
{
...
// reply->deleteLater(); does not work
_delayedRelease.reset(reply); // this works, I guess because it allows for some time to actually finish communication.
emit sendSomeDataToServer(); // this signal is processed in myclass instance; it is queued.
}
class myClass: QThread
{
signals:
void sendSomeDataToServer();
slot:
void onSendSomeDataToServer();
...
boost::shared_ptr<QNetworkReply> _delayedRelease;
QNetworkAccessManager _http;
}
myClass::myClass()
{
...
connect(this, sendSomeDataToServer, this, OnSendSomeDataToServer, Qt::ConnectionType::Queued);
}
myClass::run()
{
OnsendSomeDataToServer();
exec();
}
myclass::OnsendSomeDataToServer()
{
...
_http::post();
}
myclass::isFinished(QNetworReply* reply)
{
...
// reply->deleteLater(); does not work
_delayedRelease.reset(reply); // this works, I guess because it allows for some time to actually finish communication.
emit sendSomeDataToServer(); // this signal is processed in myclass instance; it is queued.
}
To copy to clipboard, switch view to plain text mode
This workaround does work, but I will appreciate any comments/suggestions. After all, deleteLater() is called where it is supposed to be called based on the documentation for QNetworAccessManager "isFinished" slot. If necessary I can publish the stack and provide any necessary info.
Bookmarks