Jump to content

delete c++ object causes an error?


hicz

Recommended Posts

I was working on some custom json nodes.

I wanted to export some data using json format, so I did that like as follow in cookMySop:

    UT_JSONValueMap *rootMap = new UT_JSONValueMap;
    UT_JSONValue *floatV = new UT_JSONValue(35.0);
    rootMap->append("Value1", floatV);
    rootValue->setMap(rootMap);//rootValue is an object of UT_JSONValue
    rootValue->saveToFile("G:\\test.json");

after that, I wanted to remove temp exporting data:

    rootValue->setNull();

    delete rootMap;
    rootMap = nullptr;

    delete floatV;
    floatV = nullptr;

however, using "delete" caused an error.

without "delete" everything works fine, but I am afraid there can be some memory leakage.

is there something wrong?

Link to comment
Share on other sites

Without looking into that classes, I would assume that UT_JSONValueMap->append() takes ownership of its contents and deleting rootMap also deletes the floatV data, and so you're getting a "double free" error. See if UT_JSONValueMap owns the values and if so, you don't need to delete floatV yourself.

  • Thanks 1
Link to comment
Share on other sites

41 minutes ago, Stalkerx777 said:

Without looking into that classes, I would assume that UT_JSONValueMap->append() takes ownership of its contents and deleting rootMap also deletes the floatV data, and so you're getting a "double free" error. See if UT_JSONValueMap owns the values and if so, you don't need to delete floatV yourself.

Thank you, tested that, deleting outer object and all its members were gone with it. There is no need to delete member objects manually.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...