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

Avoid memory leak in loaned-message return path #97

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
18 changes: 11 additions & 7 deletions r2r/src/subscribers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,27 @@ where
let handle_ptr = Box::into_raw(handle_box);
let ret =
rcl_return_loaned_message_from_subscription(handle_ptr, msg as *mut c_void);
if ret != RCL_RET_OK as i32 {
if ret == RCL_RET_OK as i32 {
drop(Box::from_raw(handle_ptr));
} else {
let topic_str = rcl_subscription_get_topic_name(handle_ptr);
let topic = CStr::from_ptr(topic_str);
drop(Box::from_raw(handle_ptr));

let err_str = rcutils_get_error_string();
let err_str_ptr = &(err_str.str_) as *const std::os::raw::c_char;
let error_msg = CStr::from_ptr(err_str_ptr);

let topic_str = rcl_subscription_get_topic_name(handle_ptr);
let topic = CStr::from_ptr(topic_str);

crate::log_error!(
"r2r",
// Returning a loan shouldn't fail unless one of the handles or pointers
// is invalid, both of which indicate a severe bug. Panicking is therefore
// more appropriate than leaking the loaned message.
panic!(
"rcl_return_loaned_message_from_subscription() \
failed for subscription on topic {}: {}",
topic.to_str().expect("to_str() call failed"),
error_msg.to_str().expect("to_str() call failed")
);
}
// drop(Box::from_raw(handle_ptr));
});
WrappedNativeMsg::<T>::from_loaned(loaned_msg as *mut T::CStruct, deallocator)
} else {
Expand Down