2121#include < memory>
2222#include < string>
2323
24+ #include " rcl/allocator.h"
2425#include " rcl/dynamic_message_type_support.h"
2526#include " rcl/type_hash.h"
2627#include " rcl/types.h"
@@ -76,9 +77,24 @@ DynamicMessageTypeSupport::DynamicMessageTypeSupport(
7677
7778 // NOTE(methylDragon): Not technically const correct, but since it's a const void *,
7879 // we do it anyway...
79- auto ts_impl = static_cast <const rmw_dynamic_message_type_support_impl_t *>(ts->data );
80+ auto ts_impl = static_cast <const rosidl_dynamic_message_type_support_impl_t *>(ts->data );
81+
82+ // NOTE(methylDragon): We don't destroy the rosidl_message_type_support->data since its members
83+ // are managed by the passed in SharedPtr wrapper classes. We just delete it.
84+ rosidl_message_type_support_.reset (
85+ ts,
86+ [](rosidl_message_type_support_t * ts) -> void {
87+ auto ts_impl = static_cast <const rosidl_dynamic_message_type_support_impl_t *>(ts->data );
88+ auto allocator = rcl_get_default_allocator ();
89+
90+ // These are all C allocated
91+ allocator.deallocate (ts_impl->type_hash , &allocator.state );
92+ allocator.deallocate (
93+ const_cast <rosidl_dynamic_message_type_support_impl_t *>(ts_impl), &allocator.state );
94+ allocator.deallocate (ts, &allocator.state );
95+ }
96+ );
8097
81- manage_rosidl_message_type_support_ (ts);
8298 manage_description_ (ts_impl->type_description );
8399 serialization_support_ = DynamicSerializationSupport::make_shared (ts_impl->serialization_support );
84100
@@ -136,9 +152,21 @@ DynamicMessageTypeSupport::DynamicMessageTypeSupport(
136152 throw std::runtime_error (" rosidl message type support is of the wrong type" );
137153 }
138154
139- auto ts_impl = static_cast <const rmw_dynamic_message_type_support_impl_t *>(ts->data );
155+ auto ts_impl = static_cast <const rosidl_dynamic_message_type_support_impl_t *>(ts->data );
156+
157+ // NOTE(methylDragon): We don't finalize the rosidl_message_type_support->data since its members
158+ // are managed by the passed in SharedPtr wrapper classes. We just delete it.
159+ rosidl_message_type_support_.reset (
160+ ts,
161+ [](rosidl_message_type_support_t * ts) -> void {
162+ auto ts_impl = static_cast <const rosidl_dynamic_message_type_support_impl_t *>(ts->data );
140163
141- manage_rosidl_message_type_support_ (ts);
164+ // These are allocated with new
165+ delete ts_impl->type_hash ; // Only because we should've allocated it here
166+ delete ts_impl;
167+ delete ts;
168+ }
169+ );
142170 manage_description_ (ts_impl->type_description );
143171
144172 dynamic_message_type_ = DynamicMessageType::make_shared (
@@ -237,24 +265,6 @@ DynamicMessageTypeSupport::DynamicMessageTypeSupport(
237265DynamicMessageTypeSupport::~DynamicMessageTypeSupport () {}
238266
239267
240- void
241- DynamicMessageTypeSupport::manage_rosidl_message_type_support_ (
242- rosidl_message_type_support_t * rosidl_message_type_support)
243- {
244- // NOTE(methylDragon): We don't finalize the rosidl_message_type_support->data since its members
245- // are managed by the passed in SharedPtr wrapper classes. We just delete it.
246- rosidl_message_type_support_.reset (
247- rosidl_message_type_support,
248- [](rosidl_message_type_support_t * ts) -> void {
249- auto ts_impl = static_cast <const rmw_dynamic_message_type_support_impl_t *>(ts->data );
250- delete ts_impl->type_hash ; // Only because we should've allocated it here
251- delete ts_impl;
252- delete ts;
253- }
254- );
255- }
256-
257-
258268void
259269DynamicMessageTypeSupport::manage_description_ (
260270 rosidl_runtime_c__type_description__TypeDescription * description)
@@ -299,16 +309,18 @@ DynamicMessageTypeSupport::init_rosidl_message_type_support_(
299309 throw std::runtime_error (" failed to get type hash" );
300310 }
301311
302- rmw_dynamic_message_type_support_impl_t * ts_impl = new rmw_dynamic_message_type_support_impl_t {
303- type_hash.get (), // type_hash
304- description, // type_description
305- nullptr , // NOTE(methylDragon): Not supported for now // type_description_sources
306- serialization_support->get_rosidl_serialization_support (), // serialization_support
307- dynamic_message_type->get_rosidl_dynamic_type (), // dynamic_message_type
308- dynamic_message->get_rosidl_dynamic_data () // dynamic_message
312+ rosidl_dynamic_message_type_support_impl_t * ts_impl =
313+ new rosidl_dynamic_message_type_support_impl_t {
314+ type_hash.get (), // type_hash
315+ description, // type_description
316+ nullptr , // NOTE(methylDragon): Not supported for now // type_description_sources
317+ serialization_support->get_rosidl_serialization_support (), // serialization_support
318+ dynamic_message_type->get_rosidl_dynamic_type (), // dynamic_message_type
319+ dynamic_message->get_rosidl_dynamic_data () // dynamic_message
309320 };
310321 if (!ts_impl) {
311- throw std::runtime_error (" Could not allocate rmw_dynamic_message_type_support_impl_t struct" );
322+ throw std::runtime_error (
323+ " Could not allocate rosidl_dynamic_message_type_support_impl_t struct" );
312324 return ;
313325 }
314326
@@ -320,15 +332,17 @@ DynamicMessageTypeSupport::init_rosidl_message_type_support_(
320332 ts_impl, // data
321333 get_message_typesupport_handle_function, // func
322334 // get_type_hash_func
323- rmw_dynamic_message_type_support_get_type_hash_function ,
335+ rosidl_get_dynamic_message_type_support_type_hash_function ,
324336 // get_type_description_func
325- rmw_dynamic_message_type_support_get_type_description_function ,
337+ rosidl_get_dynamic_message_type_support_type_description_function ,
326338 // get_type_description_sources_func
327- rmw_dynamic_message_type_support_get_type_description_sources_function
339+ rosidl_get_dynamic_message_type_support_type_description_sources_function
328340 },
329341 [](rosidl_message_type_support_t * ts) -> void {
330- auto ts_impl = static_cast <const rmw_dynamic_message_type_support_impl_t *>(ts->data );
331- delete ts_impl->type_hash ; // Only because we should've allocated it here
342+ auto ts_impl = static_cast <const rosidl_dynamic_message_type_support_impl_t *>(ts->data );
343+ auto allocator = rcl_get_default_allocator ();
344+ // Only because we should've allocated it here (also it's C allocated)
345+ allocator.deallocate (ts_impl->type_hash , &allocator.state );
332346 delete ts_impl;
333347 }
334348 );
0 commit comments