PDA

View Full Version : QNetworkReply app crash in QThread



migel
29th August 2011, 17:34
I create an QThread using moveToThread(), where I use a QTimer to pull stuff from network instantly.

In construct I call

_http = new HttpAdapter(this);

and in periodically function run

_http->Download();

In httpAdapter header

QNetworkReply* _reply;


void HttpAdapter::Download() {

QString url;
url += Server::get()->GetBaseUrl();
url += "get.xml";

QNetworkAccessManager * http = new QNetworkAccessManager;

QNetworkRequest request;
request.setHeader(QNetworkRequest::ContentLengthHe ader, "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::Network Error)),this, SLOT(foldersError(QNetworkReply::NetworkError)));
connect(_reply,SIGNAL(sslErrors ( const QList<QSslError> & )),this, SLOT(SslErrors( const QList<QSslError> &)));
}

The on finish...



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

_data = _reply->readAll();

The crash happen randomly, I would say in 5% of the calls, Any idea why ??

wysota
29th August 2011, 17:41
Get rid of the memory leak related to creating the access manager object at every iteration and then see if the problem persists.

migel
29th August 2011, 17:46
How do I do it ?

QNetworkAccessManager * http = new QNetworkAccessManager;

on finished

http->deleteLater() ?

I have tried


delete _reply->manager(); always crash

wysota
29th August 2011, 17:56
How do I do it ?
You rewrite the code so it doesn't leak memory. For example by reusing the same object.


always crash
Well, if "reply" is invalid then "reply->manager()" will surely be invalid as well.

migel
29th August 2011, 20:58
I have put it that to the construct


_http = new QNetworkAccessManager;


So I am using same object every call, app crashed on readAll() again after few minutes.

Any clues ?

wysota
30th August 2011, 00:40
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.