Skip to content

Commit

Permalink
Use new deletes for memory management
Browse files Browse the repository at this point in the history
Signed-off-by: methylDragon <methylDragon@gmail.com>
  • Loading branch information
methylDragon committed Mar 28, 2023
1 parent 28ce1bf commit f7f548f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ DynamicData::get_value<std::string>(rosidl_dynamic_typesupport_member_id_t id)
rosidl_dynamic_typesupport_dynamic_data_get_string_value(
get_rosidl_dynamic_data(), id, &buf, &buf_length);
auto out = std::string(buf, buf_length);
free(buf);
delete buf;
return out;
}

Expand All @@ -192,7 +192,7 @@ DynamicData::get_value<std::u16string>(rosidl_dynamic_typesupport_member_id_t id
rosidl_dynamic_typesupport_dynamic_data_get_wstring_value(
get_rosidl_dynamic_data(), id, &buf, &buf_length);
auto out = std::u16string(buf, buf_length);
free(buf);
delete buf;
return out;
}

Expand Down
10 changes: 5 additions & 5 deletions rclcpp/src/rclcpp/dynamic_typesupport/dynamic_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ DynamicData::DynamicData(const DynamicTypeBuilder::SharedPtr dynamic_type_builde
// Custom deleter
[](rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data)->void {
rosidl_dynamic_typesupport_dynamic_data_fini(rosidl_dynamic_data);
free(rosidl_dynamic_data);
delete rosidl_dynamic_data;
});
}

Expand Down Expand Up @@ -101,7 +101,7 @@ DynamicData::DynamicData(const DynamicType::SharedPtr dynamic_type)
// Custom deleter
[](rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data)->void {
rosidl_dynamic_typesupport_dynamic_data_fini(rosidl_dynamic_data);
free(rosidl_dynamic_data);
delete rosidl_dynamic_data;
});
}

Expand Down Expand Up @@ -129,7 +129,7 @@ DynamicData::DynamicData(
// Custom deleter
[](rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data)->void {
rosidl_dynamic_typesupport_dynamic_data_fini(rosidl_dynamic_data);
free(rosidl_dynamic_data);
delete rosidl_dynamic_data;
});
}

Expand Down Expand Up @@ -517,7 +517,7 @@ DynamicData::get_bounded_string_value(
rosidl_dynamic_typesupport_dynamic_data_get_bounded_string_value(
get_rosidl_dynamic_data(), id, &buf, &buf_length, string_bound);
auto out = std::string(buf, buf_length);
free(buf);
delete buf;
return out;
}

Expand All @@ -538,7 +538,7 @@ DynamicData::get_bounded_wstring_value(
rosidl_dynamic_typesupport_dynamic_data_get_bounded_wstring_value(
get_rosidl_dynamic_data(), id, &buf, &buf_length, wstring_bound);
auto out = std::u16string(buf, buf_length);
free(buf);
delete buf;
return out;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,14 @@ DynamicMessageTypeSupport::init_rosidl_message_type_support_(
}

// NOTE(methylDragon): We don't finalize the rosidl_message_type_support->data since its members
// are managed by the passed in SharedPtr wrapper classes
// are managed by the passed in SharedPtr wrapper classes. We just delete it.
rosidl_message_type_support_.reset(
new rosidl_message_type_support_t(),
[](rosidl_message_type_support_t * ts) -> void {free(const_cast<void *>(ts->data));}
[](rosidl_message_type_support_t * ts) -> void {
delete static_cast<rmw_dynamic_typesupport_impl_t *>(
const_cast<void *>(ts->data) // Feels like ts->data should be void *...
);
}
);

if (!rosidl_message_type_support_) {
Expand All @@ -259,16 +263,18 @@ DynamicMessageTypeSupport::init_rosidl_message_type_support_(
rosidl_message_type_support_->typesupport_identifier = rmw_dynamic_typesupport_c__identifier;

// NOTE(methylDragon): To populate dynamic_type and description if deferred, OUTSIDE
rosidl_message_type_support_->data = calloc(1, sizeof(rmw_dynamic_typesupport_impl_t));
rosidl_message_type_support_->data = new rmw_dynamic_typesupport_impl_t();
if (!rosidl_message_type_support_->data) {
RCUTILS_LOG_ERROR_NAMED(
rmw_dynamic_typesupport_c__identifier,
"Could not allocate rmw_dynamic_typesupport_impl_t struct");
rosidl_message_type_support_.reset();
}

rmw_dynamic_typesupport_impl_t * ts_impl =
(rmw_dynamic_typesupport_impl_t *)rosidl_message_type_support_->data;
// Again, feels like ts->data should be void *...
rmw_dynamic_typesupport_impl_t * ts_impl = static_cast<rmw_dynamic_typesupport_impl_t *>(
const_cast<void *>(rosidl_message_type_support_->data)
);

ts_impl->take_dynamic_data = middleware_can_take_dynamic_data;
ts_impl->serialization_support = serialization_support->get_rosidl_serialization_support();
Expand Down
6 changes: 3 additions & 3 deletions rclcpp/src/rclcpp/dynamic_typesupport/dynamic_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ DynamicType::DynamicType(DynamicTypeBuilder::SharedPtr dynamic_type_builder)
// Custom deleter
[](rosidl_dynamic_typesupport_dynamic_type_t * rosidl_dynamic_type)->void {
rosidl_dynamic_typesupport_dynamic_type_fini(rosidl_dynamic_type);
free(rosidl_dynamic_type);
delete rosidl_dynamic_type;
});
}

Expand All @@ -84,7 +84,7 @@ DynamicType::DynamicType(
// Custom deleter
[](rosidl_dynamic_typesupport_dynamic_type_t * rosidl_dynamic_type)->void {
rosidl_dynamic_typesupport_dynamic_type_fini(rosidl_dynamic_type);
free(rosidl_dynamic_type);
delete rosidl_dynamic_type;
});
}

Expand Down Expand Up @@ -174,7 +174,7 @@ DynamicType::init_from_description(
// Custom deleter
[](rosidl_dynamic_typesupport_dynamic_type_t * rosidl_dynamic_type)->void {
rosidl_dynamic_typesupport_dynamic_type_fini(rosidl_dynamic_type);
free(rosidl_dynamic_type);
delete rosidl_dynamic_type;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ DynamicTypeBuilder::DynamicTypeBuilder(
// Custom deleter
[](rosidl_dynamic_typesupport_dynamic_type_builder_t * rosidl_dynamic_type_builder)->void {
rosidl_dynamic_typesupport_dynamic_type_builder_fini(rosidl_dynamic_type_builder);
free(rosidl_dynamic_type_builder);
delete rosidl_dynamic_type_builder;
});
}

Expand Down Expand Up @@ -167,7 +167,7 @@ DynamicTypeBuilder::init_from_description(
// Custom deleter
[](rosidl_dynamic_typesupport_dynamic_type_builder_t * rosidl_dynamic_type_builder)->void {
rosidl_dynamic_typesupport_dynamic_type_builder_fini(rosidl_dynamic_type_builder);
free(rosidl_dynamic_type_builder);
delete rosidl_dynamic_type_builder;
});
}

Expand Down Expand Up @@ -198,7 +198,7 @@ DynamicTypeBuilder::init_from_serialization_support_(
// Custom deleter
[](rosidl_dynamic_typesupport_dynamic_type_builder_t * rosidl_dynamic_type_builder)->void {
rosidl_dynamic_typesupport_dynamic_type_builder_fini(rosidl_dynamic_type_builder);
free(rosidl_dynamic_type_builder);
delete rosidl_dynamic_type_builder;
});
}

Expand Down

0 comments on commit f7f548f

Please sign in to comment.