PDA

View Full Version : How to properly stop QNetworkRequest ?



#Dragon
27th October 2015, 18:04
Hi, I've got a problem with my HttpClient, when I'm trying to stop/cancel request my application crashes... How can I cancel it properly ?

After when I'm trying to stop by this function:


void HttpClient::stop(){
if(m_reply) {
m_reply->abort();
}
}


it crashes on: loop.exec();

in function 'wait for finish'...


void HttpClient::waitForFinish(QNetworkReply *reply)
{
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
}



QString HttpClient::get(const QString &url)
{
if(url.isEmpty()){
Logger::getInstance()->Error(trUtf8("HttpClient: GET Response <Url is empty!>"));
}
m_reply = m_manager->get(*this->generateRequest(url, false));
waitForFinish(m_reply);
if(!m_reply){
return "";
}

... code



This problem occurs sometimes, not always. I think that if, in another thread still performs and here is aborted it causes this crash...
Any idea ?

Thanks for any help ;)

anda_skoa
27th October 2015, 19:13
Since you are using a nested event loop, have you checked if you enter the get() method maybe more than once?

Also the * at this->generateRequest looks peculiar, almost like if the return value of generateRequest is QUrl* and not QUrl.

Cheers,
_

yeye_olive
28th October 2015, 15:20
You mentioned threads. Are HttpClient::stop() and HttpClient::get() executed by distinct threads? That would be a problem.