PDA

View Full Version : QNetworkAccessManager Memory leak



newgod
27th July 2012, 02:05
while(1){
QNetworkAccessManager *aa = new QNetworkAccessManager();
QNetworkReply *bb;
QNetworkRequest Request;
Request.setUrl(QUrl("http://127.0.0.1"));

bb= aa->get(Request);
bb->abort();
bb->close();
bb->deleteLater();
bb = NULL;

delete aa;
aa = NULL;
}


i have a web spider project using QNetworkAccessManager,this code is a example,run this code
you can see the memory no leak,but most people no carefully review the other information in the code's progress
use progress view or windows7 task manager ,you can see the handle-count,up up up

after a while,the progress memory Perhaps still between 30M - 60M

but the handle count has not been reduced.

if the handle count > 1,000,000,the program died

what's the problem,i try to watch QNetworkAccessManager class's code,but no help

it's QNetworkAccessManager class's bug,not my,right?

if it's my problem,how can i fix it,and where:confused:

os:win7 + qt4.8.1

ChrisW67
27th July 2012, 02:38
Since your code never returns to the Qt event loop none of the Qt mechanisms for scheduling later object deletion, opening, reading, closing sockets etc. will ever run. I am surprised that ports for sockets are not exhausted much earlier, but it's hard to say how the Qt code will react to such a pathological usage pattern. Your example is so far from a working web spider implementation it seems largely moot anyway.

You can fix it by understanding that Qt networking is done on an asynchronous basis and that those events need to be serviced.

MadMax411
27th July 2012, 09:06
In a first attempt insert the processEvents() function of QApplication in your while(1)-loop to give bb->deleteLater() a chance. And delete the line "bb = NULL".

Qt-Doc: QCoreApplication processEvents (http://qt-project.org/doc/qt-4.8/qcoreapplication.html#processEvents)

wysota
27th July 2012, 10:06
In a first attempt insert the processEvents() function of QApplication in your while(1)-loop to give bb->deleteLater() a chance. And delete the line "bb = NULL".

Qt-Doc: QCoreApplication processEvents (http://qt-project.org/doc/qt-4.8/qcoreapplication.html#processEvents)

That will not do anything good since the code aborts a request immediately after making it. The whole application is essentially a no-op.

newgod
28th July 2012, 10:21
8061
8062

this is the class file