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* ownership
Forum Updated to NodeBB v4.3 + New Features

QNetworkReply* ownership

Scheduled Pinned Locked Moved Solved General and Desktop
networkqtnetworkdeletelaterqnetworkreply
5 Posts 3 Posters 1.0k Views 2 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.
  • mistralegnaM Offline
    mistralegnaM Offline
    mistralegna
    wrote on last edited by
    #1

    Hi,

    I'm using an instance of QNetworkAccessManager to create GET and POST request, by using the corresponding get() and post() methods. These methods return a pointer to the QNetworkReply* that was created.

    I've got several questions about this created object:

    (1) Who is the owner of this QNetworkReply*? Is it the responsibility of the caller to manage the lifetime of this object? If so, does it need to be handled in the slots connected to the errorOccurred or finished signal, by calling deleteLater in these slots?
    (2) Is it possible that the QNetworkReply* never emits one of the signals mentioned in (1), leading to some QNetworkReply* that will never be destroyed?

    Thank you very much for your answer!

    jsulmJ 1 Reply Last reply
    0
    • mistralegnaM mistralegna

      Hi,

      I'm using an instance of QNetworkAccessManager to create GET and POST request, by using the corresponding get() and post() methods. These methods return a pointer to the QNetworkReply* that was created.

      I've got several questions about this created object:

      (1) Who is the owner of this QNetworkReply*? Is it the responsibility of the caller to manage the lifetime of this object? If so, does it need to be handled in the slots connected to the errorOccurred or finished signal, by calling deleteLater in these slots?
      (2) Is it possible that the QNetworkReply* never emits one of the signals mentioned in (1), leading to some QNetworkReply* that will never be destroyed?

      Thank you very much for your answer!

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

      @mistralegna said in QNetworkReply* ownership:

      QNetworkReply

      1. Caller is responsible. It is up to you how you manage its lifetime, but usually it is done like you described it.
      2. You can also store the pointer as member variable or (if you have many in parallel) in a list member variable.

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

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

        Hi,

        In addition to what @jsulm wrote for number 1, since Qt 5.14, you can QNetworkAccessManager:: setAutoDeleteReplies to handle the replies lifetime.

        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
        • mistralegnaM Offline
          mistralegnaM Offline
          mistralegna
          wrote on last edited by
          #4

          Thank you very much for your answers, @jsulm and @SGaist !

          In the case we use the setAutoDeleteReplies or the corresponding attribute at the QNetworkRequest level, what will happen if for some reason the finished signal is never emitted?

          Does it mean that in this case, the caller still needs to ensure the QNetworkReply* will be correctly destroyed at some point?

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

            AFAIK, if this does happen you likely have other bigger issues. The signal is not related to the success of the request.

            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
            1

            • Login

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