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. [Solved]Is this a safe way to delete QNetworkReply?
Forum Updated to NodeBB v4.3 + New Features

[Solved]Is this a safe way to delete QNetworkReply?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 12.8k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    stereomatching
    wrote on last edited by
    #1

    @
    void getKomica()
    {
    QNetworkAccessManager manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply
    )), this, SLOT(finished(QNetworkReply*)));
    manager->get(QNetworkRequest(QUrl("http://2cat.or.tl/~tedc21thc/live/index.php?res=553222")) );
    }

    private slots:
    void finished(QNetworkReply *reply)
    {
    reply->deleteLater();
    //do something
    }
    @

    According to the document, we should delete QNetworkReply by ourself, I call deleteLater
    in the slot from the beginning, is this safe?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AcerExtensa
      wrote on last edited by
      #2

      Docu says:

      bq. Schedules this object for deletion.
      The object will be deleted when control returns to the event loop. If the event loop is not running when this function is called (e.g. deleteLater() is called on an object before QCoreApplication::exec()), the object will be deleted once the event loop is started.
      Note that entering and leaving a new event loop (e.g., by opening a modal dialog) will not perform the deferred deletion; for the object to be deleted, the control must return to the event loop from which deleteLater() was called.
      Note: It is safe to call this function more than once; when the first deferred deletion event is delivered, any pending events for the object are removed from the event queue.

      So, if you will not touch your reply after calling deleteLater, it should be safe to do that here, i think...

      God is Real unless explicitly declared as Integer.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lgeyer
        wrote on last edited by
        #3

        Using QObject::deleteLater() is the recommended way (do not <code>delete</code> it directly within the slot).

        However, I would prefer using the object before deleting it.
        @
        void finished(QNetworkReply *reply)
        {
        // do something
        reply->deleteLater();
        }
        @

        1 Reply Last reply
        0
        • S Offline
          S Offline
          stereomatching
          wrote on last edited by
          #4

          Thanks for your answer.
          Since I don't know the slot would go back to the event loop or not
          I prefer to handle the resource by std::unique_ptr
          @
          class deleteLaterDeletor
          {
          public:
          template<typename T>
          void operator()(T *data) const
          {
          if(data) data->deleteLater();
          }
          };

          void finished(QNetworkReply *reply)
          {
          std::unique_ptr<QNetworkReply, deleteLaterDeletor> guard_reply(reply);
          //do something
          }
          @

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AcerExtensa
            wrote on last edited by
            #5

            You can do this Qt way too: "QSharedPointer":http://doc-snapshot.qt-project.org/4.8/qsharedpointer.html#QSharedPointer-3

            @QSharedPointer<MyObject> obj =
            QSharedPointer<MyObject>(new MyObject, &QObject::deleteLater);@

            bq. QSharedPointer will delete the pointer it is holding when it goes out of scope, provided no other QSharedPointer objects are referencing it.

            God is Real unless explicitly declared as Integer.

            1 Reply Last reply
            1
            • S Offline
              S Offline
              stereomatching
              wrote on last edited by
              #6

              Thanks, AcerExtensa.
              I didn't know that QSharedPointer could assign a custom deleter

              I would prefer std::unique_ptr rather than QSharedPointer in most of the cases
              because unique_ptr is cheaper, and very easy to use, just like QSharedPointer

              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