Skip to content

Commit

Permalink
Avoid memory leak in loaned-message return path
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasstarkwayve committed May 31, 2024
1 parent 2194815 commit 5d80d2b
Showing 1 changed file with 11 additions and 7 deletions.
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

0 comments on commit 5d80d2b

Please sign in to comment.