Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Correct way of manually deleting QNetworkAccessManager
QtWS25 Last Chance

Correct way of manually deleting QNetworkAccessManager

Scheduled Pinned Locked Moved General and Desktop
qnetworkaccessm
4 Posts 2 Posters 3.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    testus
    wrote on last edited by
    #1

    I have a Mainwindow that uses the QNetworkAccessManager in the constructor to check if a new version is available. The Mainwindow will stay open for many hours and because of that I would like to delete the QNetworkAccessManager after it finished its task.

    This is my implementation:

    MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow){
    	ui->setupUi(this);
    
    	manager = new QNetworkAccessManager();
    	connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(fileIsReady(QNetworkReply*)) );
    	QNetworkRequest request;
    	request.setUrl(QUrl("http://example.com/version.xml"));
    	request.setRawHeader( "User-Agent" , "Mozilla Firefox" );
    	manager->get(request);	
    }
    
    void MainWindow::fileIsReady(QNetworkReply *reply){
    	QByteArray data = reply->readAll();
    	//...read and check data...		
    	delete manager;
    }
    

    This seems to work fine. However, as soon as I start using a new QNetworkAccessManager in another Dialog I get all sorts of wierd program crashes. If I don't delete the QNetworkAccessManager manually but instead use manager = new QNetworkAccessManager(this) I don't get any crashes. Doing it that way would mean that the QNetworkAccessManager won't be deleted until the program closes which is not what I want.
    How can I manually delete the QNetworkAccessManager?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Two questions:

      1. Have you tried to use ?
      manager->deleteLater();
      manager = nullptr;
      
      1. Why do you need more managers?

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      T 1 Reply Last reply
      0
      • M mcosta

        Two questions:

        1. Have you tried to use ?
        manager->deleteLater();
        manager = nullptr;
        
        1. Why do you need more managers?
        T Offline
        T Offline
        testus
        wrote on last edited by testus
        #3

        @mcosta
        Thank you for your answer. It seems that your suggestion is working without any crashes. Am I correct with my assumption that you shouldn't use delete with Qt pointers? Will your suggestion actually free up any memory?

        About your question on why I need more managers. The user has the option to open a new Dialog and submit files. This new Dialog has its own manager.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          QObject::deleteLater() schedules the object to be deleted when there are no events related to it. So using it means "delete this object when is not used anymore".

          assigning to nullptr is to avoid to have dangling pointer so you can test

          if (manager) {
            // still used
          }
          

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved