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. Should I need to free the memory when using QJsonObject.toObject?

Should I need to free the memory when using QJsonObject.toObject?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 513 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
    Maxx Adam
    wrote on last edited by
    #1

    I have an QJsonObject from QJsonDocument like this:

    void some_function() {
      auto doc = QJsonDocument::fromJson(...);
      auto dataNode = doc.object().value("data");
      auto dataObj = dataNode.toObject();
      ...
    }
    

    Should I need to free dataNode and dataObj? Why?
    Thanks.

    J.HilkJ 1 Reply Last reply
    0
    • M Maxx Adam

      @Christian-Ehrlicher Thanks. But I have another question.
      How can I get that JsonObject from in the function. For example:

      std::shared_ptr<QJsonObject> outerObj;  //I need to get that dataObj
      auto success = parseJsonDocument(doc, outerObj);
      ...
      
      bool parseJsonDocument(QJsonDocument &doc, std::shared_ptr<QJsonObject> &obj) {
        auto dataNode = doc.object().value("data");
        auto dataObj = dataNode.toObject();
        obj = std::make_shared<QJsonObject>(dataObj);   //Is it legal?
        return true;
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #5

      @Maxx-Adam said in Should I need to free the memory when using QJsonObject.toObject?:

      How can I get that JsonObject from in the function

      Simply return it:

      QJsonObject parseJsonDocument(QJsonDocument &doc, std::shared_ptr<QJsonObject> &obj) {
        auto dataNode = doc.object().value("data");
        auto dataObj = dataNode.toObject();
        return dataObject;;
      }
      

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

      1 Reply Last reply
      4
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @Maxx-Adam said in Should I need to free the memory when using QJsonObject.toObject?:

        Should I need to free dataNode and dataObj?

        How would you free the memory of an object on the stack by your own? Please learn c++ first.

        An object on the stack is automatically deleted when the stack frame is left.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        M 1 Reply Last reply
        4
        • M Maxx Adam

          I have an QJsonObject from QJsonDocument like this:

          void some_function() {
            auto doc = QJsonDocument::fromJson(...);
            auto dataNode = doc.object().value("data");
            auto dataObj = dataNode.toObject();
            ...
          }
          

          Should I need to free dataNode and dataObj? Why?
          Thanks.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #3

          @Maxx-Adam the pitfalls of auto

          had you used proper type declarations and you can see if its heap allocated or not.

          Or if you insist in auto, * your variable and you will get a compile time error


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • Christian EhrlicherC Christian Ehrlicher

            @Maxx-Adam said in Should I need to free the memory when using QJsonObject.toObject?:

            Should I need to free dataNode and dataObj?

            How would you free the memory of an object on the stack by your own? Please learn c++ first.

            An object on the stack is automatically deleted when the stack frame is left.

            M Offline
            M Offline
            Maxx Adam
            wrote on last edited by
            #4

            @Christian-Ehrlicher Thanks. But I have another question.
            How can I get that JsonObject from in the function. For example:

            std::shared_ptr<QJsonObject> outerObj;  //I need to get that dataObj
            auto success = parseJsonDocument(doc, outerObj);
            ...
            
            bool parseJsonDocument(QJsonDocument &doc, std::shared_ptr<QJsonObject> &obj) {
              auto dataNode = doc.object().value("data");
              auto dataObj = dataNode.toObject();
              obj = std::make_shared<QJsonObject>(dataObj);   //Is it legal?
              return true;
            }
            
            jsulmJ 1 Reply Last reply
            0
            • M Maxx Adam

              @Christian-Ehrlicher Thanks. But I have another question.
              How can I get that JsonObject from in the function. For example:

              std::shared_ptr<QJsonObject> outerObj;  //I need to get that dataObj
              auto success = parseJsonDocument(doc, outerObj);
              ...
              
              bool parseJsonDocument(QJsonDocument &doc, std::shared_ptr<QJsonObject> &obj) {
                auto dataNode = doc.object().value("data");
                auto dataObj = dataNode.toObject();
                obj = std::make_shared<QJsonObject>(dataObj);   //Is it legal?
                return true;
              }
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @Maxx-Adam said in Should I need to free the memory when using QJsonObject.toObject?:

              How can I get that JsonObject from in the function

              Simply return it:

              QJsonObject parseJsonDocument(QJsonDocument &doc, std::shared_ptr<QJsonObject> &obj) {
                auto dataNode = doc.object().value("data");
                auto dataObj = dataNode.toObject();
                return dataObject;;
              }
              

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

              1 Reply Last reply
              4

              • Login

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