Results 1 to 5 of 5

Thread: QNetworkAccessManager Memory leak

  1. #1
    Join Date
    Feb 2012
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QNetworkAccessManager Memory leak

    Qt Code:
    1. while(1){
    2. QNetworkAccessManager *aa = new QNetworkAccessManager();
    3. QNetworkReply *bb;
    4. QNetworkRequest Request;
    5. Request.setUrl(QUrl("http://127.0.0.1"));
    6.  
    7. bb= aa->get(Request);
    8. bb->abort();
    9. bb->close();
    10. bb->deleteLater();
    11. bb = NULL;
    12.  
    13. delete aa;
    14. aa = NULL;
    15. }
    To copy to clipboard, switch view to plain text mode 

    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

    os:win7 + qt4.8.1
    Last edited by newgod; 27th July 2012 at 02:08. Reason: updated contents

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QNetworkAccessManager Memory leak

    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.

  3. #3

    Default Re: QNetworkAccessManager Memory leak

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkAccessManager Memory leak

    Quote Originally Posted by MadMax411 View Post
    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
    That will not do anything good since the code aborts a request immediately after making it. The whole application is essentially a no-op.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2012
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager Memory leak

    downloadmanager.cpp
    downloadmanager.h

    this is the class file

Similar Threads

  1. Qt example with memory leak
    By Squall in forum Qt Programming
    Replies: 5
    Last Post: 21st February 2011, 10:07
  2. memory leak - where?
    By Tomasz in forum Newbie
    Replies: 36
    Last Post: 5th September 2010, 23:47
  3. Memory leak
    By yxtx1984 in forum Qt Programming
    Replies: 4
    Last Post: 26th February 2010, 11:13
  4. memory leak
    By mattia in forum Newbie
    Replies: 18
    Last Post: 16th January 2008, 10:22
  5. Memory leak
    By vvbkumar in forum General Programming
    Replies: 4
    Last Post: 2nd September 2006, 15:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.