I was reading this question, and here the jsoncpp CharReaderBuilder::newCharReader()
function returns a pointer to a dynamically created CharReader
object, which can then be used to parse a JSON.
I understand in that question the OP should have freed the returned pointer once it was used, since it was created on the heap. But, I am confused about whether, if we never store the returned pointer since it is for 1-time-use only, and write something like this:
Json::Value root;
Json::CharReaderBuilder().newCharReader()->parse(serverResponse.response,
serverResponse.response + serverResponse.size - 1, &root, &Json::String());
Will this still cause a memory leak? If so, then in this case, I am not storing the pointer, so I cannot really call free()
or delete
to free that location of memory since I need a reference for that.
I guess I could somehow wrap the entire thing around the C++ unique_ptr
thing, but I think this should not cause any memory leak. Am I correct?
Edit: From the comments, it seems this will still cause a memory leak. So, how should I delete this pointer since I currently have no reference to it? Am I forced to create a reference and store the pointer if I want to avoid a memory leak? Is there no other way?