Skip to content

Do not throw exceptions inside a custom deleter #534

@anhosi

Description

@anhosi

In message_memory_strategy.hpp The following custom deleter is used for a std::shared_ptr:

auto serialized_msg = std::shared_ptr<rcl_serialized_message_t>(msg,
[](rmw_serialized_message_t * msg) {
auto ret = rmw_serialized_message_fini(msg);
delete msg;
if (ret != RCL_RET_OK) {
rclcpp::exceptions::throw_from_rcl_error(ret, "leaking memory");
}
});
.

According to https://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr a deleter is forbidden to throw exceptions.

4-5) Uses the specified deleter d as the deleter. The expression d(ptr) must be well formed, have well-defined behavior and not throw any exceptions. The construction of d and of the stored deleter from d must not throw exceptions.

I understand that is is rather unlikely for this deleter to throw but IMHO it would be better to avoid throwing at all within the deleter.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions