Skip to content

Commit

Permalink
fix: fix memeory leak if Dart_PostCObject_DL failed to send the message
Browse files Browse the repository at this point in the history
  • Loading branch information
littleGnAl committed Mar 16, 2023
1 parent 00d1388 commit 55f322f
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/iris_event.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <memory>
#include <map>
#include <vector>

#include "iris_life_cycle_observer.h"

Expand Down Expand Up @@ -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<Dart_CObject *> 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<uint8_t *>(malloc(abufferLength));
uint8_t *abuffer = static_cast<uint8_t *>(malloc(abufferLength));
memcpy(abuffer, obuffer, abufferLength);

Dart_CObject *cbuffer = new Dart_CObject;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -182,7 +193,6 @@ class DartMessageHandlerManager : public DartMessageHandlerBase
std::map<Dart_Port, std::unique_ptr<DartMessageHandler>> dartMessageHandlerMap_;
};

// DartMessageHandler *dartMessageHandler_ = nullptr;
DartMessageHandlerManager *dartMessageHandlerManager_ = nullptr;

// Initialize `dart_api_dl.h`
Expand All @@ -193,7 +203,6 @@ EXPORT intptr_t InitDartApiDL(void *data)
dartMessageHandlerManager_ = new DartMessageHandlerManager();
}

// dartMessageHandler_ = new DartMessageHandler();
return dartMessageHandlerManager_->InitDartApiDL(data);
}

Expand Down

0 comments on commit 55f322f

Please sign in to comment.