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