From b864ee7e61128f6807ba75b3c10274d6bb0f3a3a Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Mon, 10 Jul 2023 21:54:21 +1200 Subject: [PATCH] Fix leak when switching channels (flutter/engine#41827) As noticed in https://github.com/flutter/flutter/issues/126209 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide] and the [C++, Objective-C, Java style guides]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test-exempt. See [testing the engine] for instructions on writing and running engine tests. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I signed the [CLA]. - [x] All existing and new tests are passing. --- .../flutter/shell/platform/linux/fl_method_channel.cc | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/engine/src/flutter/shell/platform/linux/fl_method_channel.cc b/engine/src/flutter/shell/platform/linux/fl_method_channel.cc index d4c45ed920..23ec1b062f 100644 --- a/engine/src/flutter/shell/platform/linux/fl_method_channel.cc +++ b/engine/src/flutter/shell/platform/linux/fl_method_channel.cc @@ -75,10 +75,6 @@ static void channel_closed_cb(gpointer user_data) { g_autoptr(FlMethodChannel) self = FL_METHOD_CHANNEL(user_data); self->channel_closed = TRUE; - // Clear the messenger so that disposing the channel will not clear the - // messenger's mapped channel, since `channel_closed_cb` means the messenger - // has abandoned this channel. - self->messenger = nullptr; // Disconnect handler. if (self->method_call_handler_destroy_notify != nullptr) { @@ -92,10 +88,9 @@ static void channel_closed_cb(gpointer user_data) { static void fl_method_channel_dispose(GObject* object) { FlMethodChannel* self = FL_METHOD_CHANNEL(object); - if (self->messenger != nullptr) { - fl_binary_messenger_set_message_handler_on_channel( - self->messenger, self->name, nullptr, nullptr, nullptr); - } + // Note we don't have to clear the handler in messenger as it holds + // a reference to this object so the following code is only run after + // the messenger has closed the channel already. g_clear_object(&self->messenger); g_clear_pointer(&self->name, g_free);