Skip to content

Commit 29577da

Browse files
committed
Move dynamic type support struct to rosidl and fix mem issues
Signed-off-by: methylDragon <[email protected]>
1 parent 000f788 commit 29577da

File tree

7 files changed

+66
-63
lines changed

7 files changed

+66
-63
lines changed

rclcpp/include/rclcpp/dynamic_typesupport/dynamic_message_type_support.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace dynamic_typesupport
4343
* support struct, instead of rcl_dynamic_message_type_support_handle_init,
4444
* because this class will manage the lifetimes for you.
4545
*
46-
* Do NOT call rcl_dynamic_message_type_support_handle_fini!!
46+
* Do NOT call rcl_dynamic_message_type_support_handle_destroy!!
4747
*
4848
* This class:
4949
* - Manages the lifetime of the raw pointer.
@@ -178,10 +178,6 @@ class DynamicMessageTypeSupport : public std::enable_shared_from_this<DynamicMes
178178
RCLCPP_PUBLIC
179179
DynamicMessageTypeSupport();
180180

181-
RCLCPP_PUBLIC
182-
void
183-
manage_rosidl_message_type_support_(rosidl_message_type_support_t * rosidl_message_type_support);
184-
185181
RCLCPP_PUBLIC
186182
void
187183
manage_description_(rosidl_runtime_c__type_description__TypeDescription * description);

rclcpp/include/rclcpp/dynamic_typesupport/dynamic_serialization_support.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ class DynamicSerializationSupport : public std::enable_shared_from_this<DynamicS
4646
RCLCPP_SMART_PTR_ALIASES_ONLY(DynamicSerializationSupport)
4747

4848
// CONSTRUCTION ==================================================================================
49+
RCLCPP_PUBLIC
50+
DynamicSerializationSupport();
51+
4952
/// Get the rmw middleware implementation specific serialization support (configured by name)
5053
RCLCPP_PUBLIC
51-
explicit DynamicSerializationSupport(const std::string & serialization_library_name = "");
54+
explicit DynamicSerializationSupport(const std::string & serialization_library_name);
5255

5356
/// Assume ownership of raw pointer
5457
RCLCPP_PUBLIC
@@ -97,10 +100,6 @@ class DynamicSerializationSupport : public std::enable_shared_from_this<DynamicS
97100
RCLCPP_DISABLE_COPY(DynamicSerializationSupport)
98101

99102
std::shared_ptr<rosidl_dynamic_typesupport_serialization_support_t> rosidl_serialization_support_;
100-
101-
private:
102-
RCLCPP_PUBLIC
103-
DynamicSerializationSupport();
104103
};
105104

106105

rclcpp/src/rclcpp/dynamic_typesupport/dynamic_message.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ DynamicMessage::DynamicMessage(const DynamicMessageTypeBuilder::SharedPtr dynami
6868
rosidl_dynamic_data,
6969
// Custom deleter
7070
[](rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data)->void {
71-
rosidl_dynamic_typesupport_dynamic_data_fini(rosidl_dynamic_data);
72-
delete rosidl_dynamic_data;
71+
rosidl_dynamic_typesupport_dynamic_data_destroy(rosidl_dynamic_data);
7372
});
7473
}
7574

@@ -103,8 +102,7 @@ DynamicMessage::DynamicMessage(const DynamicMessageType::SharedPtr dynamic_type)
103102
rosidl_dynamic_data,
104103
// Custom deleter
105104
[](rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data)->void {
106-
rosidl_dynamic_typesupport_dynamic_data_fini(rosidl_dynamic_data);
107-
delete rosidl_dynamic_data;
105+
rosidl_dynamic_typesupport_dynamic_data_destroy(rosidl_dynamic_data);
108106
});
109107
}
110108

@@ -131,8 +129,7 @@ DynamicMessage::DynamicMessage(
131129
rosidl_dynamic_data,
132130
// Custom deleter
133131
[](rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data)->void {
134-
rosidl_dynamic_typesupport_dynamic_data_fini(rosidl_dynamic_data);
135-
delete rosidl_dynamic_data;
132+
rosidl_dynamic_typesupport_dynamic_data_destroy(rosidl_dynamic_data);
136133
});
137134
}
138135

rclcpp/src/rclcpp/dynamic_typesupport/dynamic_message_type.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ DynamicMessageType::DynamicMessageType(DynamicMessageTypeBuilder::SharedPtr dyna
6060
rosidl_dynamic_type,
6161
// Custom deleter
6262
[](rosidl_dynamic_typesupport_dynamic_type_t * rosidl_dynamic_type)->void {
63-
rosidl_dynamic_typesupport_dynamic_type_fini(rosidl_dynamic_type);
64-
delete rosidl_dynamic_type;
63+
rosidl_dynamic_typesupport_dynamic_type_destroy(rosidl_dynamic_type);
6564
});
6665
}
6766

@@ -85,8 +84,7 @@ DynamicMessageType::DynamicMessageType(
8584
rosidl_dynamic_type,
8685
// Custom deleter
8786
[](rosidl_dynamic_typesupport_dynamic_type_t * rosidl_dynamic_type)->void {
88-
rosidl_dynamic_typesupport_dynamic_type_fini(rosidl_dynamic_type);
89-
delete rosidl_dynamic_type;
87+
rosidl_dynamic_typesupport_dynamic_type_destroy(rosidl_dynamic_type);
9088
});
9189
}
9290

@@ -171,8 +169,7 @@ DynamicMessageType::init_from_description(
171169
rosidl_dynamic_type,
172170
// Custom deleter
173171
[](rosidl_dynamic_typesupport_dynamic_type_t * rosidl_dynamic_type)->void {
174-
rosidl_dynamic_typesupport_dynamic_type_fini(rosidl_dynamic_type);
175-
delete rosidl_dynamic_type;
172+
rosidl_dynamic_typesupport_dynamic_type_destroy(rosidl_dynamic_type);
176173
});
177174
}
178175

rclcpp/src/rclcpp/dynamic_typesupport/dynamic_message_type_builder.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ DynamicMessageTypeBuilder::DynamicMessageTypeBuilder(
7272
rosidl_dynamic_type_builder,
7373
// Custom deleter
7474
[](rosidl_dynamic_typesupport_dynamic_type_builder_t * rosidl_dynamic_type_builder)->void {
75-
rosidl_dynamic_typesupport_dynamic_type_builder_fini(rosidl_dynamic_type_builder);
76-
delete rosidl_dynamic_type_builder;
75+
rosidl_dynamic_typesupport_dynamic_type_builder_destroy(rosidl_dynamic_type_builder);
7776
});
7877
}
7978

@@ -165,8 +164,7 @@ DynamicMessageTypeBuilder::init_from_description(
165164
rosidl_dynamic_type_builder,
166165
// Custom deleter
167166
[](rosidl_dynamic_typesupport_dynamic_type_builder_t * rosidl_dynamic_type_builder)->void {
168-
rosidl_dynamic_typesupport_dynamic_type_builder_fini(rosidl_dynamic_type_builder);
169-
delete rosidl_dynamic_type_builder;
167+
rosidl_dynamic_typesupport_dynamic_type_builder_destroy(rosidl_dynamic_type_builder);
170168
});
171169
}
172170

@@ -201,8 +199,7 @@ DynamicMessageTypeBuilder::init_from_serialization_support_(
201199
rosidl_dynamic_type_builder,
202200
// Custom deleter
203201
[](rosidl_dynamic_typesupport_dynamic_type_builder_t * rosidl_dynamic_type_builder)->void {
204-
rosidl_dynamic_typesupport_dynamic_type_builder_fini(rosidl_dynamic_type_builder);
205-
delete rosidl_dynamic_type_builder;
202+
rosidl_dynamic_typesupport_dynamic_type_builder_destroy(rosidl_dynamic_type_builder);
206203
});
207204
}
208205

rclcpp/src/rclcpp/dynamic_typesupport/dynamic_message_type_support.cpp

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
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(
237265
DynamicMessageTypeSupport::~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-
258268
void
259269
DynamicMessageTypeSupport::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
);

rclcpp/src/rclcpp/dynamic_typesupport/dynamic_serialization_support.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
using rclcpp::dynamic_typesupport::DynamicSerializationSupport;
2929

3030
// CONSTRUCTION ====================================================================================
31+
DynamicSerializationSupport::DynamicSerializationSupport()
32+
: DynamicSerializationSupport::DynamicSerializationSupport("") {};
33+
3134
DynamicSerializationSupport::DynamicSerializationSupport(
3235
const std::string & serialization_library_name)
3336
: rosidl_serialization_support_(nullptr)

0 commit comments

Comments
 (0)