-
Notifications
You must be signed in to change notification settings - Fork 435
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
Fully initialise parameter msgs #358
Conversation
Otherwise valgrind errors that data is uninitialised
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.
Just from reviewing the code I don't see where the problem is coming from. I would feel better if I could see which exact change
@@ -70,6 +70,9 @@ NodeParameters::set_parameters_atomically( | |||
|
|||
// TODO(jacquelinekay): handle parameter constraints | |||
rcl_interfaces::msg::SetParametersResult result; | |||
result.successful = false; |
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.
I don't see why this is necessary. In the if
branch the result variable is overridden anyway. In the else
branch the boolean is being set.
@@ -70,6 +70,9 @@ NodeParameters::set_parameters_atomically( | |||
|
|||
// TODO(jacquelinekay): handle parameter constraints | |||
rcl_interfaces::msg::SetParametersResult result; | |||
result.successful = false; | |||
result.reason = ""; |
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.
std::string
are already initialized int their constructor.
{ | ||
value_.type = rcl_interfaces::msg::ParameterType::PARAMETER_NOT_SET; | ||
value_.bool_value = false; | ||
value_.integer_value = 0; | ||
value_.double_value = 0.0; |
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.
The idea is that each constructor only initialized the type as well as the type-specific value. All other value fields should never be accessed - therefore they are not initialized for a tiny improvement to the performance.
value_.integer_value = 0; | ||
value_.double_value = 0.0; | ||
value_.string_value = ""; | ||
value_.bytes_value = std::vector<uint8_t>(); |
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.
std::string
s as well as other std
containers are initialized in their constructor.
} | ||
|
||
ParameterVariant::ParameterVariant(const std::string & name, const bool bool_value) | ||
: name_(name) | ||
: ParameterVariant(name) |
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.
If we don't initialize the unused value fields this as well as the added optional argument to the default constructor wouldn't be necessary.
Same below.
the minimal change required to prevent the valgrind error for that specific test is initialising As I mentioned the other changes were just to be conservative because I'm not sure it what context the fields are initialised. It seems that they're not necessary for the most part, and that even when they are left uninitialised it was on purpose for performance |
I start to remember implementing #37 a while back. We should certainly try to fix the valgrind error. I am just not yet sure how to do it 😉 |
Yeah I was also asking that in the initial post. If we're both thinking it then I'll go in that direction instead (specifying defaults in the message definition); it will be clearer to users what the defaults are then |
👍 For this message it makes sense since it is expected that the code using it will only touch some of the fields. |
OK ros2/rcl_interfaces#18 adds the defaults to the message definition instead. This PR will likely be closed in favour of that one |
* Refactor utility functions to api module Signed-off-by: Anas Abou Allaban <aabouallaban@pm.me>
The first commit (bf3314c) prevents the following valgrind error on
test_local_parameters__rmw_fastrtps_cpp.set_parameter_if_not_set
intest_rclcpp
The second commit (5b93dfe) explicitly initialises some messages in other locations that I saw but I can't say for sure if it's necessary or not.
It might be more appropriate for us to set default values in the message definitions themselves: I can adapt this PR if that's the case.