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

Update SignalHandler get_global_signal_handler to avoid complex types in static memory #2316

Merged
merged 3 commits into from
Sep 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rclcpp/src/rclcpp/signal_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ SignalHandler::get_logger()
SignalHandler &
SignalHandler::get_global_signal_handler()
{
static SignalHandler signal_handler;
static SignalHandler & signal_handler = *new SignalHandler();
Copy link
Contributor

Choose a reason for hiding this comment

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

I will just point out that there is a tradeoff here; now all memory tracing tools (like valgrind) are going to find a memory leak for all rclcpp programs. I'm not sure that is enough of a problem to block putting this in, but it is a downside.

Is there any place we could safely delete it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe that memory debuggers are aware of this pattern. They actually won't report this as a memory leak because there's always a valid reference to it during execution until final termination due to the statically allocated reference. As such for all times during the program's execution this memory is reachable.

Copy link
Member

Choose a reason for hiding this comment

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

There's no good time to delete it, and though I don't love it either, it is the recommended solution in the google c++ style guide. 🤷

Copy link
Member

Choose a reason for hiding this comment

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

The alternative would be to ensure this class is trivially destructible which isn't possible given that it needs to store things like the rclcpp::Logger which itself stores a string, a std::thread, etc... So I think this is the best solution unfortunately.

I will point out it is a recommended style, not a correctness thing. What's there today is valid and (as far as we can see) correct code, but the issue is if a bug works its way in, this pattern (non-trivial static objects) makes diagnosing harder, hence the recommendation. So I think it's a valid thing to change.

return signal_handler;
}

Expand Down