QNetworkReply app crash in QThread
I create an QThread using moveToThread(), where I use a QTimer to pull stuff from network instantly.
In construct I call
Code:
_http = new HttpAdapter(this);
and in periodically function run
In httpAdapter header
Code:
QNetworkReply* _reply;
Code:
void HttpAdapter::Download() {
url += Server::get()->GetBaseUrl();
url += "get.xml";
QNetworkAccessManager * http = new QNetworkAccessManager;
QNetworkRequest request;
request.setHeader(QNetworkRequest::ContentLengthHeader, "0");
request.
setUrl(QUrl::fromUserInput(url
));
_reply = http->get(request);
connect(_reply,SIGNAL(finished()),this, SLOT(Finished()));
connect(_reply,SIGNAL(readyRead()),this, SLOT(ReadyData()));
connect(_reply,SIGNAL(error(QNetworkReply::NetworkError)),this, SLOT(foldersError(QNetworkReply::NetworkError)));
connect(_reply,SIGNAL(sslErrors ( const QList<QSslError> & )),this, SLOT(SslErrors( const QList<QSslError> &)));
}
The on finish...
Code:
void HttpAdapter::Finished() {
DEBUGME_MSG("finished");
int httpCode = _reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt();
if (httpCode != 200) {
INFO_DEBUGME
("HTTP CODE " + QString::number(httpCode
));
return;
}
if (!_reply->isFinished()) {
return;
}
if (!_reply->isReadable()) {
return;
}
_data = _reply->readAll();
_reply->deleteLater();
if (_data.isEmpty()) {
INFO_DEBUGME("DATA EMPTY");
return;
}
//success
}
And THE CRASH IS ON
Code:
_data = _reply->readAll();
The crash happen randomly, I would say in 5% of the calls, Any idea why ??
Re: QNetworkReply app crash in QThread
Get rid of the memory leak related to creating the access manager object at every iteration and then see if the problem persists.
Re: QNetworkReply app crash in QThread
How do I do it ?
QNetworkAccessManager * http = new QNetworkAccessManager;
on finished
http->deleteLater() ?
I have tried
Code:
delete _reply->manager();
always crash
Re: QNetworkReply app crash in QThread
Quote:
Originally Posted by
migel
How do I do it ?
You rewrite the code so it doesn't leak memory. For example by reusing the same object.
Well, if "reply" is invalid then "reply->manager()" will surely be invalid as well.
Re: QNetworkReply app crash in QThread
I have put it that to the construct
Code:
_http = new QNetworkAccessManager;
So I am using same object every call, app crashed on readAll() again after few minutes.
Any clues ?
Re: QNetworkReply app crash in QThread
You need to provide us with more complete code. I'm especially interested in when and how you call this Download() method. My guess is that when finished() is emitted, your reply is already invalid or overwritten by something else.