-
Notifications
You must be signed in to change notification settings - Fork 93
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
Fix memory leak that string not deleted. #224
Fix memory leak that string not deleted. #224
Conversation
Signed-off-by: Chen.Lihui <lihui.chen@sony.com>
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.
Thanks @iuhilnehc-ynos for figuring this out and proposing a fix. This looks good to me, the only thing I wonder about is the use of a unique_ptr
like this — and that’s purely as a matter of style. I’m not a good judge of C++ style, so I’d like to wait for a review from one of the other people who regularly contribute here before I press merge.
I don't see how this fixes a leak. Before |
I figured that’s taken care of by the
to me it looks like that means the destructor runs exactly once for each successful call to |
Using take with the same message repeatedly should not leak. I can try to dig in the current typesupport implementation, but the patch proposed here doesn't seem to be correct (it's hiding a leak in |
as @ivanpauno mentioned previously, I do not see how this fixes the memory leak either. As far as i see here is to change allocation area into heap from stack. |
Let me try to explain what happened before.
ParticipantEntitiesInfo msg;
rmw_take(impl->common.sub, &msg, &taken, nullptr)
and then if there is another message need to take immediately, it means rmw_take(impl->common.sub, &msg, &taken, nullptr) The So we need to call the destructor of ParticipantEntitiesInfo every time after That's my understanding. |
Actually, using stack is also working but it needs to change the original logic.
|
rmw_cyclonedds_cpp/src/rmw_node.cpp
Outdated
@@ -551,11 +551,12 @@ static void handle_ParticipantEntitiesInfo(dds_entity_t reader, void * arg) | |||
{ | |||
static_cast<void>(reader); | |||
rmw_context_impl_t * impl = static_cast<rmw_context_impl_t *>(arg); | |||
ParticipantEntitiesInfo msg; | |||
auto msg = std::unique_ptr<ParticipantEntitiesInfo>(new ParticipantEntitiesInfo); |
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 agree on this kind of change finally, I'll update it with make_unique
.
call buffer size with 1. https://github.com/eclipse-cyclonedds/cyclonedds/blob/eb12a8da00b403a1458513873faae8807e13fcd7/src/core/ddsc/src/dds_read.c#L65 rmw_cyclonedds/rmw_cyclonedds_cpp/src/serdata.cpp Lines 446 to 458 in 1bc587e
besides, sertopic_rmw_realloc_samples is not implemented yet.
I am not sure how reallocation happens in this implementation...i may be missing something. |
string reallocated is happened at
&msg -> ... -> TypeSupport::deserializeROSmessage() -> (msg is ROS_TYPE_MESSAGE, is array, array size is 0) -> get_submessage_array_deserialize ->
-> continue to call
->
The related code is so much, I just provided a little here. Hope these can make you understand more clearly. |
It's not related to
Please refer to rmw_cyclonedds/rmw_cyclonedds_cpp/src/rmw_node.cpp Lines 550 to 560 in 1bc587e
|
okay i was digging deeper 😄 this helps, thanks |
I wonder if reallocation is expected to happen here,
it seems that bounded strings are intentionally used, |
It's known that the passed message is a valid object (i.e.: it was at least default constructed).
That example shows the real problem. The issue is that: MessageType msg;
rmw_take(subscription, &msg, &taken, nullptr);
rmw_take(subscription, &msg, &taken, nullptr); is leaking when message is not a POD data structure (because of the typesupport corrupting the previous object). |
I think that the problem is that the typesupport is calling placement new over the memory of an existing object: rmw_cyclonedds/rmw_cyclonedds_cpp/include/rmw_cyclonedds_cpp/TypeSupport_impl.hpp Line 538 in 1bc587e
rmw_cyclonedds/rmw_cyclonedds_cpp/include/rmw_cyclonedds_cpp/TypeSupport_impl.hpp Line 279 in 1bc587e
and thus leaks. |
If not calling |
why? |
rmw_cyclonedds/rmw_cyclonedds_cpp/include/rmw_cyclonedds_cpp/TypeSupport_impl.hpp Lines 276 to 278 in 1bc587e
this filed for std::string here is resized at rmw_cyclonedds/rmw_cyclonedds_cpp/include/rmw_cyclonedds_cpp/TypeSupport_impl.hpp Lines 445 to 446 in 1bc587e
std::string .
|
the code doing the resizing is broken, and the code doing placement new is broken. e.g.:
MessageB msg;
msg.data.resize(10);
rmw_take(subscription, &msg, taken, null); // here a leak, because placement new is corrupting the old memory I see this was copied from |
@iuhilnehc-ynos for the moment, we can merge a fix as the one you proposed above:
We can follow-up in the broken typesupport implementation later. |
Maybe, the current one is also fine. Just add a TODO explaining why the workaround is needed. |
Thanks for your support. I prefer to use |
Signed-off-by: Chen.Lihui <lihui.chen@sony.com>
Sorry, there will be a problem(not calling constructor after reset()), so I updated it by using the latter workaround. |
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.
LGTM
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.
LGTM, @eboasson what do you think?
@ivanpauno LGTM too! |
Thanks for the fix @iuhilnehc-ynos ! |
This reverts commit 416c01d. Signed-off-by: Chen.Lihui <lihui.chen@sony.com>
This reverts commit 416c01d. Signed-off-by: Chen.Lihui <lihui.chen@sony.com>
Signed-off-by: Chen.Lihui <lihui.chen@sony.com>
Related to ros2/rcl#721
memory leak log
The patch is to make sure deleting ParticipantEntitiesInfo after calling
rmw_take
each time.Signed-off-by: Chen.Lihui lihui.chen@sony.com