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. It seems setParent will cause crash
Forum Updated to NodeBB v4.3 + New Features

It seems setParent will cause crash

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 692 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.
  • MozzieM Offline
    MozzieM Offline
    Mozzie
    wrote on last edited by
    #1

    Hi,

    Recently, I meet a very strange problem.
    I am using Qt5.12 on windows10 with VS2017

    my code is:
    HttpClient.h

    #ifndef HTTPCLIENT_H
    #define HTTPCLIENT_H
    
    #include <QObject>
    #include <QNetworkAccessManager>
    #include <QNetworkReply>
    
    
    class HttpClient : public QObject
    {
    	Q_OBJECT
    protected:
    	QNetworkAccessManager* m_networkManager = nullptr;
    	QNetworkReply* m_reply = nullptr;
    public:
    	explicit HttpClient(QObject *parent = nullptr);
    	virtual ~HttpClient();
    
    	inline QNetworkReply* reply() const {return m_reply;}
    
    	void start();
    signals:
    	void finished();
    };
    
    #endif // HTTPCLIENT_H
    
    

    HttpClient.cpp

    #include "HttpClient.h"
    
    #include "NetworkAccessManager.h"
    
    #include <QDebug>
    
    
    HttpClient::HttpClient(QObject *parent) : QObject(parent)
    {
    
    }
    
    HttpClient::~HttpClient()
    {
    #ifdef QT_DEBUG
    	qDebug() << __FUNCTION__;
    #endif
    
    	//// when use m_networkManager = new NetworkAccessManager();
    //	if (m_networkManager) {
    //		m_networkManager->deleteLater();
    //	}
    }
    
    void HttpClient::start()
    {
    #ifdef QT_DEBUG
    	qDebug() << __FUNCTION__;
    #endif
    	{
    		if (!m_networkManager) {
    			// without "this" as parent, delete m_networkManager in destructor will not crash
    			//m_networkManager = new NetworkAccessManager();
    			// with "this" as parent, not delete m_networkManager in destructor will crash
    			m_networkManager = new NetworkAccessManager(this);
    		}
    		QNetworkRequest request(QUrl("https://qt.io/"));
    		m_reply = m_networkManager->get(request);
    
    		// with "this" as parent, not delete m_networkManager in destructor, not setParent(m_reply),
    		// delete HttpClient after finished() signal will not crash
    		setParent(m_reply);
    		connect(m_reply, &QNetworkReply::finished, this, &HttpClient::finished);
    
    	}
    }
    
    

    call

    	{
    		HttpClient* client = new HttpClient;
    		connect(client, &HttpClient::finished, this, [this, client](){
    			qDebug() << __FUNCTION__ << __LINE__;
    			auto reply = client->reply();
    			//client->deleteLater();
    			reply->deleteLater();
    		});
    		client->start();
    	}
    

    after calling m_networkManager = new NetworkAccessManager(this);, the m_networkManager will be a child of this(HttpClient), Then after calling setParent(m_reply); the HttpClient wiil be a child of reply, after the reply is finished, I called reply->deleteLater();, this should delete the three object one by one, but it will cause crash. If I do not use HttpClient as parent of the m_networkManager OR not use setParent(m_reply); then delete the m_networkManager in the destructor, it will not crash.

    Is there anyone know why did this happened? Thank you.

    jsulmJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Mozzie said in It seems setParent will cause crash:

      Is there anyone know why did this happened? Thank you.

      You create a circular dependency HttpClient -> NetworkAccessManager -> QNetworkReply.

      But what do you try to achieve with this really bogus setParent() - call?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      MozzieM 1 Reply Last reply
      1
      • MozzieM Mozzie

        Hi,

        Recently, I meet a very strange problem.
        I am using Qt5.12 on windows10 with VS2017

        my code is:
        HttpClient.h

        #ifndef HTTPCLIENT_H
        #define HTTPCLIENT_H
        
        #include <QObject>
        #include <QNetworkAccessManager>
        #include <QNetworkReply>
        
        
        class HttpClient : public QObject
        {
        	Q_OBJECT
        protected:
        	QNetworkAccessManager* m_networkManager = nullptr;
        	QNetworkReply* m_reply = nullptr;
        public:
        	explicit HttpClient(QObject *parent = nullptr);
        	virtual ~HttpClient();
        
        	inline QNetworkReply* reply() const {return m_reply;}
        
        	void start();
        signals:
        	void finished();
        };
        
        #endif // HTTPCLIENT_H
        
        

        HttpClient.cpp

        #include "HttpClient.h"
        
        #include "NetworkAccessManager.h"
        
        #include <QDebug>
        
        
        HttpClient::HttpClient(QObject *parent) : QObject(parent)
        {
        
        }
        
        HttpClient::~HttpClient()
        {
        #ifdef QT_DEBUG
        	qDebug() << __FUNCTION__;
        #endif
        
        	//// when use m_networkManager = new NetworkAccessManager();
        //	if (m_networkManager) {
        //		m_networkManager->deleteLater();
        //	}
        }
        
        void HttpClient::start()
        {
        #ifdef QT_DEBUG
        	qDebug() << __FUNCTION__;
        #endif
        	{
        		if (!m_networkManager) {
        			// without "this" as parent, delete m_networkManager in destructor will not crash
        			//m_networkManager = new NetworkAccessManager();
        			// with "this" as parent, not delete m_networkManager in destructor will crash
        			m_networkManager = new NetworkAccessManager(this);
        		}
        		QNetworkRequest request(QUrl("https://qt.io/"));
        		m_reply = m_networkManager->get(request);
        
        		// with "this" as parent, not delete m_networkManager in destructor, not setParent(m_reply),
        		// delete HttpClient after finished() signal will not crash
        		setParent(m_reply);
        		connect(m_reply, &QNetworkReply::finished, this, &HttpClient::finished);
        
        	}
        }
        
        

        call

        	{
        		HttpClient* client = new HttpClient;
        		connect(client, &HttpClient::finished, this, [this, client](){
        			qDebug() << __FUNCTION__ << __LINE__;
        			auto reply = client->reply();
        			//client->deleteLater();
        			reply->deleteLater();
        		});
        		client->start();
        	}
        

        after calling m_networkManager = new NetworkAccessManager(this);, the m_networkManager will be a child of this(HttpClient), Then after calling setParent(m_reply); the HttpClient wiil be a child of reply, after the reply is finished, I called reply->deleteLater();, this should delete the three object one by one, but it will cause crash. If I do not use HttpClient as parent of the m_networkManager OR not use setParent(m_reply); then delete the m_networkManager in the destructor, it will not crash.

        Is there anyone know why did this happened? Thank you.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Mozzie said in It seems setParent will cause crash:

        HttpClient

        Why should HttpClient be child of the reply?
        This sounds wrong.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        MozzieM 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          @Mozzie said in It seems setParent will cause crash:

          Is there anyone know why did this happened? Thank you.

          You create a circular dependency HttpClient -> NetworkAccessManager -> QNetworkReply.

          But what do you try to achieve with this really bogus setParent() - call?

          MozzieM Offline
          MozzieM Offline
          Mozzie
          wrote on last edited by Mozzie
          #4

          @Christian-Ehrlicher

          Thank you. I want to delete the HttpClient object automatically after the QNetworkReply called deleteLater()

          1 Reply Last reply
          0
          • jsulmJ jsulm

            @Mozzie said in It seems setParent will cause crash:

            HttpClient

            Why should HttpClient be child of the reply?
            This sounds wrong.

            MozzieM Offline
            MozzieM Offline
            Mozzie
            wrote on last edited by
            #5

            @jsulm
            I want to delete the HttpClient object automatically after the QNetworkReply called deleteLater()

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Then connect deleteLater() to QNetworkReply::destroyed()

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              MozzieM 1 Reply Last reply
              2
              • Christian EhrlicherC Christian Ehrlicher

                Then connect deleteLater() to QNetworkReply::destroyed()

                MozzieM Offline
                MozzieM Offline
                Mozzie
                wrote on last edited by
                #7

                @Christian-Ehrlicher
                Thank you very much.

                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