Results 1 to 5 of 5

Thread: When to delete QNetworkAccessManager pointer?

  1. #1
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default When to delete QNetworkAccessManager pointer?

    Hi all,

    I am using QNetworkAccessManager in order to download a file from the Internet in the following way:

    Qt Code:
    1. void MainWindow::readUrlResource(const QString &url) {
    2. QNetworkRequest request;
    3. request.setUrl(QUrl(url));
    4.  
    5. QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
    6.  
    7. //Get notified when the download completes
    8. connect(networkManager, SIGNAL(finished(QNetworkReply*)),
    9. this, SLOT(urlResourceDownloadCompleted(QNetworkReply*)));
    10.  
    11. networkManager->get(request);
    12. }
    To copy to clipboard, switch view to plain text mode 

    The function above is part of my main window (inheriting from QMainWindow) and it is what is sent as "this" to QNetworkAccessManager constructor. Also, the urlResourceDownloadCompleted() slot looks like this:

    Qt Code:
    1. void MainWindow::urlResourceDownloadCompleted(QNetworkReply *reply) {
    2. //TODO: Handle the case when an error occurs during download
    3.  
    4. qDebug() << "Download completed signal received";
    5.  
    6. reply->deleteLater();
    7. }
    To copy to clipboard, switch view to plain text mode 

    I am wondering whether I need to explicitly delete networkManager in some way or will QMainWindow (parent to it) take care of this eventually? I will have QMainWindow visible for a long time on screen so if it is it taking care of deletion, I wouldn't want to wait for so long, as I might want to create more networkManagers during this time.

    If I need to delete the networkManager pointer manually, which is the best place to do so?

    I am just trying my best to avoid potential memory leak due to not deleting networkManager pointer.

    Thank you in advance. I am very new to Qt and I have some minor experience in C++ but I am having much fun so far and would like to learn thing properly.

    Kind Regards,
    Veroslav

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: When to delete QNetworkAccessManager pointer?

    Quote Originally Posted by veroslav View Post
    I am wondering whether I need to explicitly delete networkManager in some way or will QMainWindow (parent to it) take care of this eventually? I will have QMainWindow visible for a long time on screen so if it is it taking care of deletion, I wouldn't want to wait for so long, as I might want to create more networkManagers during this time.
    Qt take care of deleting objects which have parents when a parent will be deleted. So, there won't be memory leak.

    Quote Originally Posted by veroslav View Post
    If I need to delete the networkManager pointer manually, which is the best place to do so?

    I am just trying my best to avoid potential memory leak due to not deleting networkManager pointer.

    Thank you in advance. I am very new to Qt and I have some minor experience in C++ but I am having much fun so far and would like to learn thing properly.

    Kind Regards,
    Veroslav
    Why don't you use _one_ access manager for your main window (class member)?
    Last edited by spirit; 29th August 2012 at 08:58.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: When to delete QNetworkAccessManager pointer?

    Thank you for your fast reply, spirit.

    I thought about having an instance of it (as a pointer) as a class member but was not sure whether it was a good idea to have it occupying memory throughout program execution as I only needed to use it a few times during that time. Just trying to be as effective as possible.

    So I would change it to something like this, if I understood it correctly:

    Qt Code:
    1. //mainwindow.h
    2. class MainWindow : public QMainWindow {
    3. public:
    4. explicit MainWindow(QWidget *parent = 0);
    5. ~MainWindow();
    6. private:
    7. QNetworkAccessManager *networkManager;
    8. };
    9.  
    10. //mainwindow.cpp
    11. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
    12. networkManager = new QNetworkAccessManager();
    13. }
    14.  
    15. MainWindow::~MainWindow() {
    16. delete networkManager;
    17. }
    To copy to clipboard, switch view to plain text mode 

    And then just use the pointer in my readUrlResource() function?

    Thank you very much.

    EDITED:

    Or perhaps even better let the Qt deal with deletion:

    Qt Code:
    1. //mainwindow.cpp
    2. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
    3. networkManager = new QNetworkAccessManager(this);
    4. }
    To copy to clipboard, switch view to plain text mode 

    So no need for destructor.
    Last edited by veroslav; 29th August 2012 at 09:41.

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: When to delete QNetworkAccessManager pointer?

    Quote Originally Posted by veroslav View Post
    EDITED:

    Or perhaps even better let the Qt deal with deletion:

    Qt Code:
    1. //mainwindow.cpp
    2. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
    3. networkManager = new QNetworkAccessManager(this);
    4. }
    To copy to clipboard, switch view to plain text mode 

    So no need for destructor.
    In this case the access manager will be destroyed when the mainwindow is destroyed. So, you can simply make access manager as a member of the mainwindow.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. The following user says thank you to spirit for this useful post:

    veroslav (30th August 2012)

  6. #5
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: When to delete QNetworkAccessManager pointer?

    Thank you for the confirmation, this is how I am going to implement it now.

    Kind Regards,
    Veroslav

Similar Threads

  1. Replies: 2
    Last Post: 17th October 2011, 00:25
  2. Replies: 1
    Last Post: 4th December 2010, 18:20
  3. Replies: 4
    Last Post: 19th February 2009, 12:10
  4. Best Practice - delete pointer QAbstractButton in QMessageBox
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2008, 13:43
  5. Delete auto pointer from QMultiMap
    By phillip_Qt in forum Qt Programming
    Replies: 5
    Last Post: 3rd December 2007, 18:29

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.