-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TelematicPlugin: Memory leak #599
Conversation
@@ -238,7 +240,10 @@ namespace TelematicBridge | |||
} | |||
else | |||
{ | |||
json["payload"] = StringToJson(cJSON_Print(msg->payload)); | |||
// Render a cJSON entity to text for transfer/storage. Free the char* when finished. | |||
char * payloadPtr = cJSON_Print(msg->payload); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we replace this with a smart pointer to remove the need for free?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It only returns a char* Type. How do we put this in smart pointer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please address sonar code smells and update PR description. It seems to be a copy of a previous issue.
@@ -238,7 +240,9 @@ namespace TelematicBridge | |||
} | |||
else | |||
{ | |||
json["payload"] = StringToJson(cJSON_Print(msg->payload)); | |||
// Render a cJSON entity to text for transfer/storage. Free the char* when finished. | |||
auto payloadPtr = std::make_unique<char*>(cJSON_Print(msg->payload)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently std::make_unique
is sort of a wrapper for new
(https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique) and actually creates a new object from the passed in parameter. This is likely why we still see the memory leak with this in. Apparently this is the more appropriate way to create a unique pointer from the returned char*
std::unique_ptr<char, decltype(std::free) *> payloadPtr{ cJSON_Print(msg->payload), std::free };
Could you try this and see if it works?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See review
Quality Gate passedIssues Measures |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, what happened to the smart pointer stuff?
The smart pointer with free works, but I do not have solution to address the code smell.
PR Details
Description
Fix memory leaks issues with several variables created with malloc or new.
Related Issue
#575
Motivation and Context
How Has This Been Tested?
Local integration test
Types of changes
Checklist:
V2XHUB Contributing Guide