From 55f322fd202c361b4cc78c1fb060109518e918aa Mon Sep 17 00:00:00 2001 From: littleGnAl Date: Fri, 17 Mar 2023 00:48:58 +0800 Subject: [PATCH] fix: fix memeory leak if Dart_PostCObject_DL failed to send the message --- src/iris_event.cc | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/iris_event.cc b/src/iris_event.cc index e31c0e3..cbd681b 100644 --- a/src/iris_event.cc +++ b/src/iris_event.cc @@ -5,6 +5,7 @@ #include #include +#include #include "iris_life_cycle_observer.h" @@ -79,13 +80,13 @@ class DartMessageHandler : public DartMessageHandlerBase Dart_CObject dbuffer; dbuffer.type = Dart_CObject_kArray; dbuffer.value.as_array.length = param->buffer_count; - Dart_CObject **type_data_array = - new Dart_CObject *[param->buffer_count]; + + std::vector external_objs; for (unsigned int i = 0; i < param->buffer_count; i++) { const void *obuffer = param->buffer[i]; unsigned int abufferLength = param->length[i]; - uint8_t *abuffer = reinterpret_cast(malloc(abufferLength)); + uint8_t *abuffer = static_cast(malloc(abufferLength)); memcpy(abuffer, obuffer, abufferLength); Dart_CObject *cbuffer = new Dart_CObject; @@ -96,9 +97,9 @@ class DartMessageHandler : public DartMessageHandlerBase cbuffer->value.as_external_typed_data.peer = abuffer; cbuffer->value.as_external_typed_data.callback = Finalizer; - type_data_array[i] = cbuffer; + external_objs.push_back(cbuffer); } - dbuffer.value.as_array.values = type_data_array; + dbuffer.value.as_array.values = external_objs.data(); Dart_CObject *c_event_data_arr[] = {&c_event, &c_data, &dbuffer}; Dart_CObject c_on_event_data; @@ -108,15 +109,25 @@ class DartMessageHandler : public DartMessageHandlerBase sizeof(c_event_data_arr) / sizeof(c_event_data_arr[0]); if (exit_flag_ == 0) - Dart_PostCObject_DL(dart_send_port_, &c_on_event_data); + { + bool result = Dart_PostCObject_DL(dart_send_port_, &c_on_event_data); + // Need free the external typed data if failed to send, + // see https://github.com/dart-lang/sdk/issues/47270 + if (!result) + { + for (auto *it : external_objs) + { + Finalizer(nullptr, it->value.as_external_typed_data.peer); + } + } + } if (param->buffer_count != 0) { - for (intptr_t i = 0; i < param->buffer_count; ++i) + for (auto *it : external_objs) { - delete type_data_array[i]; + delete it; } - delete[] type_data_array; } } else @@ -182,7 +193,6 @@ class DartMessageHandlerManager : public DartMessageHandlerBase std::map> dartMessageHandlerMap_; }; -// DartMessageHandler *dartMessageHandler_ = nullptr; DartMessageHandlerManager *dartMessageHandlerManager_ = nullptr; // Initialize `dart_api_dl.h` @@ -193,7 +203,6 @@ EXPORT intptr_t InitDartApiDL(void *data) dartMessageHandlerManager_ = new DartMessageHandlerManager(); } - // dartMessageHandler_ = new DartMessageHandler(); return dartMessageHandlerManager_->InitDartApiDL(data); }