From 416c01d45dba80bd713d38fd4cc6360efb7448f5 Mon Sep 17 00:00:00 2001 From: Chen Lihui Date: Thu, 27 Aug 2020 03:11:43 +0800 Subject: [PATCH] Fix memory leak that string not deleted. (#224) Signed-off-by: Chen.Lihui --- rmw_cyclonedds_cpp/src/rmw_node.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/rmw_cyclonedds_cpp/src/rmw_node.cpp b/rmw_cyclonedds_cpp/src/rmw_node.cpp index 4df2c276..724a40fe 100644 --- a/rmw_cyclonedds_cpp/src/rmw_node.cpp +++ b/rmw_cyclonedds_cpp/src/rmw_node.cpp @@ -550,12 +550,21 @@ static void handle_ParticipantEntitiesInfo(dds_entity_t reader, void * arg) { static_cast(reader); rmw_context_impl_t * impl = static_cast(arg); - ParticipantEntitiesInfo msg; bool taken; - while (rmw_take(impl->common.sub, &msg, &taken, nullptr) == RMW_RET_OK && taken) { - // locally published data is filtered because of the subscription QoS - impl->common.graph_cache.update_participant_entities(msg); - } + do { + // TODO(iuhilnehc-ynos): Fix memory leak that string not deleted. (#224) + // This is a workaround to make sure calling destructor of ParticipantEntitiesInfo + // after calling rmw_take each time, otherwise, there will be a memory leak while + // deserializing a long enough buffer into a string member of NodeEntitiesInfo + // in ParticipantEntitiesInfo. + ParticipantEntitiesInfo msg; + if (rmw_take(impl->common.sub, &msg, &taken, nullptr) == RMW_RET_OK && taken) { + // locally published data is filtered because of the subscription QoS + impl->common.graph_cache.update_participant_entities(msg); + } else { + break; + } + } while (1); } static void handle_DCPSParticipant(dds_entity_t reader, void * arg)