Open fgalan opened 1 year ago
Based on other parts of the code, something like this should fix it:
for (rapidjson::Value::ConstValueIterator iter2 = iter->value.Begin(); iter2 != iter->value.End(); ++iter2)
{
ContextElementResponse* cerP = new ContextElementResponse();
ncrP->contextElementResponseVector.vec.push_back(cerP);
if (parseContextElementResponse(ciP, iter2, cerP, oeP) == false)
{
ncrP->contextElementResponseVector.release();
return false;
}
}
ncrP->contextElementResponseVector.release();
return true;
}
Based on other parts of the code, something like this should fix it:
for (rapidjson::Value::ConstValueIterator iter2 = iter->value.Begin(); iter2 != iter->value.End(); ++iter2) { ContextElementResponse* cerP = new ContextElementResponse(); ncrP->contextElementResponseVector.vec.push_back(cerP); if (parseContextElementResponse(ciP, iter2, cerP, oeP) == false) { ncrP->contextElementResponseVector.release(); return false; } } ncrP->contextElementResponseVector.release(); return true; }
I'm afraid is not a valid solution...
Considering the snipped in its context https://github.com/telefonicaid/fiware-orion/blob/master/src/lib/jsonParseV2/parseNotification.cpp#L113, note that the purpose is to fill the ncrP
vector to be used by the caller of parseNotificationData() so we cannot release just after parsing.
Hi,@fgalan.
I've gone through this issue #4261, According to the description, the new
operator allocates memory that should be eventually freed to avoid memory leaks. I propose the introduction of std::unique_ptr
to oversee the memory allocation and deallocation for ContextElementResponse
objects in theparseNotificationData
function. The smart pointer ensures automatic memory deallocation when the object goes out of scope, ensuring proper memory management and avoiding potential memory leaks. Please provide your opinion and confirm my understanding.
Looking forward to your guidance on this!
Hi @fgalan, I proposed the below code modifications and understanding. Please confirm my understanding. Thanks
std::unique_ptr
, which automatically handle memory deallocation when the object goes out of scope.std:unique_ptr
is used to manage the memory allocated for ContextElementResponse
.get ()
method is used to obtain the raw pointer for adding it to the vector. std:unique_ptr cerP
goes out of scope, it will automatically release the allocated memory, preventing memory leaks.Proposed Modifications:-
// Modified Code
for (rapidjson::Value::ConstValueIterator iter2 = iter->value.Begin(); iter2 != iter->value.End(); ++iter2)
{
std::unique_ptr<ContextElementResponse> cerP(new ContextElementResponse());
ncrP->contextElementResponseVector.vec.push_back(cerP.get());
if (parseContextElementResponse(ciP, iter2, cerP.get(), oeP) == false)
{
return false;
}
// The unique_ptr cerP will automatically release memory when it goes out of scope
}
Looking forward to your guidance on this!
Thank you for you feedback!
The usage of std::unique_ptr
could be a good idea, but it should be applied to the whole codebase, which is a big task that goes out of the scope of this issue.
Using std::unique_ptr
in just one place would introduce a lack of homogeneity in the code. It seems that the "unbalanced" new
is known (check above), which use to be the hardest part when debugging memory leaks. So now it is just a matter of adding the right delete
in the proper place.
A leak has been found in the
POST /v2/op/notify
operation. To raise the problem:you'll get:
In the log we can see:
The relevant code in parseNotification.cpp is:
It is not critical given that, as far as I know, nobody is actually using the
POST /v2/op/notify
operation.