From f7f548ff5a66812a3cec1bdf8827f78e8fef52e0 Mon Sep 17 00:00:00 2001 From: methylDragon Date: Tue, 28 Mar 2023 10:54:49 -0700 Subject: [PATCH] Use new deletes for memory management Signed-off-by: methylDragon --- .../detail/dynamic_data_impl.hpp | 4 ++-- .../rclcpp/dynamic_typesupport/dynamic_data.cpp | 10 +++++----- .../dynamic_message_type_support.cpp | 16 +++++++++++----- .../rclcpp/dynamic_typesupport/dynamic_type.cpp | 6 +++--- .../dynamic_typesupport/dynamic_type_builder.cpp | 6 +++--- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/rclcpp/include/rclcpp/dynamic_typesupport/detail/dynamic_data_impl.hpp b/rclcpp/include/rclcpp/dynamic_typesupport/detail/dynamic_data_impl.hpp index d2e77247ae..b27c237130 100644 --- a/rclcpp/include/rclcpp/dynamic_typesupport/detail/dynamic_data_impl.hpp +++ b/rclcpp/include/rclcpp/dynamic_typesupport/detail/dynamic_data_impl.hpp @@ -178,7 +178,7 @@ DynamicData::get_value(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; } @@ -192,7 +192,7 @@ DynamicData::get_value(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; } diff --git a/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_data.cpp b/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_data.cpp index 02601199c0..e53d21b7df 100644 --- a/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_data.cpp +++ b/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_data.cpp @@ -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; }); } @@ -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; }); } @@ -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; }); } @@ -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; } @@ -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; } diff --git a/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_message_type_support.cpp b/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_message_type_support.cpp index 479be09b52..86c106afeb 100644 --- a/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_message_type_support.cpp +++ b/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_message_type_support.cpp @@ -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(ts->data));} + [](rosidl_message_type_support_t * ts) -> void { + delete static_cast( + const_cast(ts->data) // Feels like ts->data should be void *... + ); + } ); if (!rosidl_message_type_support_) { @@ -259,7 +263,7 @@ 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, @@ -267,8 +271,10 @@ DynamicMessageTypeSupport::init_rosidl_message_type_support_( 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( + const_cast(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(); diff --git a/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_type.cpp b/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_type.cpp index a189d88d53..bfe593a2cf 100644 --- a/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_type.cpp +++ b/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_type.cpp @@ -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; }); } @@ -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; }); } @@ -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; }); } diff --git a/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_type_builder.cpp b/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_type_builder.cpp index fb959b3b8b..f0f438e683 100644 --- a/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_type_builder.cpp +++ b/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_type_builder.cpp @@ -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; }); } @@ -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; }); } @@ -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; }); }