diff --git a/rmw_fastrtps_cpp/src/rmw_client.cpp b/rmw_fastrtps_cpp/src/rmw_client.cpp index d2e0b5971..9b5b5aef4 100644 --- a/rmw_fastrtps_cpp/src/rmw_client.cpp +++ b/rmw_fastrtps_cpp/src/rmw_client.cpp @@ -191,6 +191,11 @@ rmw_create_client( info->writer_guid_ = info->request_publisher_->getGuid(); rmw_client = rmw_client_allocate(); + if (!rmw_client) { + RMW_SET_ERROR_MSG("failed to allocate memory for client"); + goto fail; + } + rmw_client->implementation_identifier = eprosima_fastrtps_identifier; rmw_client->data = info; rmw_client->service_name = reinterpret_cast( @@ -204,7 +209,6 @@ rmw_create_client( return rmw_client; fail: - if (info != nullptr) { if (info->request_publisher_ != nullptr) { Domain::removePublisher(info->request_publisher_); @@ -233,13 +237,16 @@ rmw_create_client( } delete info; + info = nullptr; } - if (rmw_client->service_name != nullptr) { - rmw_free(const_cast(rmw_client->service_name)); - rmw_client->service_name = nullptr; + if (nullptr != rmw_client) { + if (rmw_client->service_name != nullptr) { + rmw_free(const_cast(rmw_client->service_name)); + rmw_client->service_name = nullptr; + } + rmw_client_free(rmw_client); } - rmw_client_free(rmw_client); return nullptr; }