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

QNetworkReply* ownership

Scheduled Pinned Locked Moved Solved General and Desktop
networkqtnetworkdeletelaterqnetworkreply
5 Posts 3 Posters 811 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.
  • M Offline
    M Offline
    mistralegna
    wrote on 15 Jul 2022, 11:41 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!

    J 1 Reply Last reply 15 Jul 2022, 11:48
    0
    • M mistralegna
      15 Jul 2022, 11:41

      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!

      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 15 Jul 2022, 11:48 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
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 15 Jul 2022, 11:56 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
        • M Offline
          M Offline
          mistralegna
          wrote on 15 Jul 2022, 12:21 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
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 15 Jul 2022, 12:57 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

            2/5

            15 Jul 2022, 11:48

            • Login

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