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



  • @
    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?



  • 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...



  • 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();
    }
    @



  • 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
    }
    @



  • 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.



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.