Skip to content
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

Merged
merged 7 commits into from
Apr 16, 2024
Merged

Conversation

dan-du-car
Copy link
Collaborator

@dan-du-car dan-du-car commented Apr 9, 2024

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

  • Defect fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that cause existing functionality to change)

Checklist:

  • I have added any new packages to the sonar-scanner.properties file
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
    V2XHUB Contributing Guide
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@dan-du-car dan-du-car requested review from paulbourelly999 and removed request for paulbourelly999 April 9, 2024 17:44
@dan-du-car dan-du-car changed the title V2xHub and carma cloud integration: TCM connection refused. TelematicPlugin: Memory leak Apr 9, 2024
@dan-du-car dan-du-car marked this pull request as draft April 9, 2024 17:47
@dan-du-car dan-du-car self-assigned this Apr 12, 2024
@dan-du-car dan-du-car marked this pull request as ready for review April 12, 2024 20:05
@@ -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);
Copy link
Contributor

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?

Copy link
Collaborator Author

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?

Copy link
Contributor

@paulbourelly999 paulbourelly999 left a 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));
Copy link
Contributor

@paulbourelly999 paulbourelly999 Apr 15, 2024

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?

Copy link
Contributor

@paulbourelly999 paulbourelly999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See review

Copy link

sonarcloud bot commented Apr 15, 2024

Copy link
Contributor

@paulbourelly999 paulbourelly999 left a 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.

@dan-du-car dan-du-car merged commit 798326e into develop Apr 16, 2024
3 of 4 checks passed
@dan-du-car dan-du-car deleted the fix_carma_cloud_intergration_tcm branch April 16, 2024 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants