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. QNetworkReply not deleting

QNetworkReply not deleting

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 635 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.
  • I Offline
    I Offline
    Ivan Megazoo
    wrote on last edited by
    #1

    Hello! I'm trying to delete QNetworkReply after manager's "finished" signal is emitted. When this signal is emitted again, i get deleted QNetworkReply triggered too.

    I expect old reply to be deleted and only new one triggering signal. What can i do?
    Thanks!

    t_manager.h:

    #include <QObject>
    #include <QNetworkAccessManager>
    
    class T_Manager : public QNetworkAccessManager
    {
        Q_OBJECT
    public:
      T_Manager();
      ~T_Manager();
    
      bool set(QString host_url, int port, QString cert_path);
      void exchangeGet(QString URI);
    
    signals:
      void receivedData(QString);
      void receivedError(QString);
    
    private:
      QSslConfiguration ssl_config_;
      QUrl host_url_;
      int host_port_;
    
    private slots:
      void managerFinished(QNetworkReply* reply);
    };
    
    

    t_manager.cpp:

    
    #include "t_manager.h"
    #include <QNetworkReply>
    
    
    T_Manager::T_Manager()
    {
    }
    
    T_Manager::~T_Manager()
    {
    }
    
    bool T_Manager::set(QString host_url, int port, QString cert_path)
    {
      if(host_url.isEmpty() || cert_path.isEmpty())
        return false;
    
      host_url_.setUrl("https://" + host_url);
      host_port_ = port;
    
      QList<QSslCertificate> certs = QSslCertificate::fromPath(cert_path);
      if(certs.size() <= 0 || !host_url_.isValid())
        return false;
    
      ssl_config_.setCaCertificates(certs);
      return true;
    }
    
    void T_Manager::exchangeGet(QString URI)
    {
      QUrl request_url = QUrl(host_url_.toString() + URI);
      QNetworkRequest request(request_url);
    
      request.setSslConfiguration(ssl_config_);
    
      QNetworkReply *reply = this->get(request);
      connect(this, SIGNAL(finished(QNetworkReply*)), this, SLOT(managerFinished(QNetworkReply*)));
    }
    
    void T_Manager::managerFinished(QNetworkReply *reply)
    {
      if(reply->error())
      {
        emit receivedError(reply->errorString());
        reply->disconnect();
        reply->deleteLater();
        return;
      }
    
      QByteArray bytes = reply->readAll();
      QString received_str = QString::fromUtf8(bytes.data(), bytes.size());
      int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
      if(statusCode == 200){
        emit receivedData(received_str);
      }
      reply->disconnect();
      reply->deleteLater();
    }
    
    
    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #5

      @JonB no Qt does not forbid you to do that.

      @Ivan-Megazoo do that connection in your constructor and only there.

      Beside that, you are already deleting the reply in the slot so why are you trying to delete it even more ?

      On a side note, why are you subclassing QNetworkAccessManager ? Your T_Manager class should rather be in the "has a" rather than "is a".

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • CKurduC Offline
        CKurduC Offline
        CKurdu
        wrote on last edited by
        #2

        @Ivan-Megazoo said in QNetworkReply not deleting:

        connect(this, SIGNAL(finished(QNetworkReply*)), this, SLOT(managerFinished(QNetworkReply*)));

        The problem does not seem about two reply object, I think problem is related connecting two times.

        You can try this.

        connect(this, SIGNAL(finished(QNetworkReply*)), this, SLOT(managerFinished(QNetworkReply*)),Qt::UniqueConnection );
        

        You reap what you sow it

        1 Reply Last reply
        2
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by
          #3

          Note sure if this helps but we do this:

          QNetworkReply *reply = somefunctionthatreturnsthereply();
          QObject::connect(reply, SIGNAL(finished()), reply, SLOT(deleteLater()));
          

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          0
          • I Ivan Megazoo

            Hello! I'm trying to delete QNetworkReply after manager's "finished" signal is emitted. When this signal is emitted again, i get deleted QNetworkReply triggered too.

            I expect old reply to be deleted and only new one triggering signal. What can i do?
            Thanks!

            t_manager.h:

            #include <QObject>
            #include <QNetworkAccessManager>
            
            class T_Manager : public QNetworkAccessManager
            {
                Q_OBJECT
            public:
              T_Manager();
              ~T_Manager();
            
              bool set(QString host_url, int port, QString cert_path);
              void exchangeGet(QString URI);
            
            signals:
              void receivedData(QString);
              void receivedError(QString);
            
            private:
              QSslConfiguration ssl_config_;
              QUrl host_url_;
              int host_port_;
            
            private slots:
              void managerFinished(QNetworkReply* reply);
            };
            
            

            t_manager.cpp:

            
            #include "t_manager.h"
            #include <QNetworkReply>
            
            
            T_Manager::T_Manager()
            {
            }
            
            T_Manager::~T_Manager()
            {
            }
            
            bool T_Manager::set(QString host_url, int port, QString cert_path)
            {
              if(host_url.isEmpty() || cert_path.isEmpty())
                return false;
            
              host_url_.setUrl("https://" + host_url);
              host_port_ = port;
            
              QList<QSslCertificate> certs = QSslCertificate::fromPath(cert_path);
              if(certs.size() <= 0 || !host_url_.isValid())
                return false;
            
              ssl_config_.setCaCertificates(certs);
              return true;
            }
            
            void T_Manager::exchangeGet(QString URI)
            {
              QUrl request_url = QUrl(host_url_.toString() + URI);
              QNetworkRequest request(request_url);
            
              request.setSslConfiguration(ssl_config_);
            
              QNetworkReply *reply = this->get(request);
              connect(this, SIGNAL(finished(QNetworkReply*)), this, SLOT(managerFinished(QNetworkReply*)));
            }
            
            void T_Manager::managerFinished(QNetworkReply *reply)
            {
              if(reply->error())
              {
                emit receivedError(reply->errorString());
                reply->disconnect();
                reply->deleteLater();
                return;
              }
            
              QByteArray bytes = reply->readAll();
              QString received_str = QString::fromUtf8(bytes.data(), bytes.size());
              int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
              if(statusCode == 200){
                emit receivedData(received_str);
              }
              reply->disconnect();
              reply->deleteLater();
            }
            
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #4

            @Ivan-Megazoo said in QNetworkReply not deleting:

            When this signal is emitted again, i get deleted QNetworkReply triggered too.

            I wish I understood this! :)

            If you mean: you call exchangeGet() a second time then, as @CKurdu says, you will execute

             connect(this, SIGNAL(finished(QNetworkReply*)), this, SLOT(managerFinished(QNetworkReply*)));
            

            a second time. Unless Qt prevents duplicates (I don't know), your slot will get called twice, is that what you mean? Why are you doing the connect() in this function, instead of outside?

            1 Reply Last reply
            2
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @JonB no Qt does not forbid you to do that.

              @Ivan-Megazoo do that connection in your constructor and only there.

              Beside that, you are already deleting the reply in the slot so why are you trying to delete it even more ?

              On a side note, why are you subclassing QNetworkAccessManager ? Your T_Manager class should rather be in the "has a" rather than "is a".

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              3
              • I Offline
                I Offline
                Ivan Megazoo
                wrote on last edited by
                #6

                @SGaist @JonB yea, thanks.
                Connecting same signal and slot multiple times was the problem.

                @SGaist i was subclassing, because i'm not always sure why it was bad idea. At the time i was doing it, i thought it's not worst architecture solution.

                JonBJ 1 Reply Last reply
                0
                • I Ivan Megazoo

                  @SGaist @JonB yea, thanks.
                  Connecting same signal and slot multiple times was the problem.

                  @SGaist i was subclassing, because i'm not always sure why it was bad idea. At the time i was doing it, i thought it's not worst architecture solution.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #7

                  @Ivan-Megazoo
                  What @SGaist is suggesting is that your T_Manager class has a QNetworkAccessManager as one thing inside it, but it does not perform the function of a QNetworkAccessManager itself (it does much different) and therefore better is a has a QNetworkAccessManager rather than is a QNetworkAccessManager, which is what sub-classing implies.

                  So he is suggesting you rearrange your architecture so that T_Manager does not derive from/sub-class QNetworkManager, instead it would have a QNetworkManager m_netman member variable, and you do network operations via that member variable.

                  1 Reply Last reply
                  1

                  • Login

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