Skip to content

Commit

Permalink
Merge pull request #47 from v-kiniv/fix/memory-leaks
Browse files Browse the repository at this point in the history
Allocate message buffer properly, fix memory leaks
  • Loading branch information
v-kiniv authored Jan 23, 2025
2 parents fa93e85 + 4c24ee8 commit 4898d98
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 47 deletions.
1 change: 0 additions & 1 deletion include/rws/typesupport_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ namespace rws

using rosidl_typesupport_introspection_cpp::MessageMembers;

const size_t MSG_MEM_BLOCK_SIZE = 1024;
extern const char * ts_identifier;

/// Load the type support library for the given type.
Expand Down
55 changes: 9 additions & 46 deletions src/typesupport_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,54 +172,17 @@ std::string message_type_to_ros1_style(const std::string & ros2_msg_type)
return s;
}

static void allocate_string_members(const MessageMembers * members, uint8_t * buf) {
std::string str;
size_t str_capacity = str.capacity();
for (size_t i = 0; i < str_capacity + 1; i++) {
str += "a";
}

std::wstring wstr;
size_t wstr_capacity = wstr.capacity();
for (size_t i = 0; i < wstr_capacity + 1; i++) {
wstr += L"a";
}

for (uint32_t i = 0; i < members->member_count_; ++i) {
const auto member = members->members_ + i;

if (member->type_id_ == rosidl_typesupport_introspection_cpp::ROS_TYPE_STRING) {
if (member->is_array_) {
memcpy(
buf + member->offset_, new std::vector<std::string>(), sizeof(std::vector<std::string>));
} else {
memcpy(buf + member->offset_, new std::string(str), sizeof(std::string));
}
} else if (member->type_id_ == rosidl_typesupport_introspection_cpp::ROS_TYPE_WSTRING) {
if (member->is_array_) {
memcpy(
buf + member->offset_, new std::vector<std::wstring>(),
sizeof(std::vector<std::wstring>));
} else {
memcpy(buf + member->offset_, new std::wstring(wstr), sizeof(std::wstring));
}
} else if(member->type_id_ == rosidl_typesupport_introspection_cpp::ROS_TYPE_MESSAGE) {
auto sub_members = static_cast<const MessageMembers *>(member->members_->data);
if (!member->is_array_) {
allocate_string_members(sub_members, buf);
}
}
}
}

std::shared_ptr<void> allocate_message(const MessageMembers * members)
{
uint8_t * buf = static_cast<uint8_t *>(malloc(members->size_of_ + MSG_MEM_BLOCK_SIZE));
memset(buf, 0, MSG_MEM_BLOCK_SIZE);

allocate_string_members(members, buf);

return std::shared_ptr<void>(buf);
void * buf = new uint8_t[members->size_of_];
members->init_function(buf, rosidl_runtime_cpp::MessageInitialization::ZERO);
return std::shared_ptr<void>(
buf,
[members](void * p)
{
members->fini_function(p);
delete[] reinterpret_cast<uint8_t *>(p);
});
}

} // namespace rws

0 comments on commit 4898d98

Please sign in to comment.