Skip to content

Commit e5a7ca5

Browse files
Handle leak of message handle when no engine present (flutter#18157)
* Handle leak of message handle when no engine present * Move callback in wrong location
1 parent d3bde19 commit e5a7ca5

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

shell/platform/linux/fl_binary_messenger.cc

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ struct _FlBinaryMessengerResponseHandle {
4545
const FlutterPlatformMessageResponseHandle* response_handle;
4646
};
4747

48-
static void engine_weak_notify_cb(gpointer user_data, GObject* object) {
49-
FlBinaryMessenger* self = FL_BINARY_MESSENGER(user_data);
50-
self->engine = nullptr;
51-
}
52-
5348
static FlBinaryMessengerResponseHandle* response_handle_new(
5449
const FlutterPlatformMessageResponseHandle* response_handle) {
5550
FlBinaryMessengerResponseHandle* handle =
@@ -64,6 +59,14 @@ static void response_handle_free(FlBinaryMessengerResponseHandle* handle) {
6459
g_free(handle);
6560
}
6661

62+
G_DEFINE_AUTOPTR_CLEANUP_FUNC(FlBinaryMessengerResponseHandle,
63+
response_handle_free);
64+
65+
static void engine_weak_notify_cb(gpointer user_data, GObject* object) {
66+
FlBinaryMessenger* self = FL_BINARY_MESSENGER(user_data);
67+
self->engine = nullptr;
68+
}
69+
6770
static gboolean fl_binary_messenger_platform_message_callback(
6871
FlEngine* engine,
6972
const gchar* channel,
@@ -145,12 +148,15 @@ G_MODULE_EXPORT gboolean fl_binary_messenger_send_response(
145148
g_return_val_if_fail(FL_IS_BINARY_MESSENGER(self), FALSE);
146149
g_return_val_if_fail(response_handle != nullptr, FALSE);
147150

151+
// Take reference to ensure it is freed
152+
g_autoptr(FlBinaryMessengerResponseHandle) owned_response_handle =
153+
response_handle;
154+
148155
if (self->engine == nullptr)
149156
return TRUE;
150157

151158
gboolean result = fl_engine_send_platform_message_response(
152-
self->engine, response_handle->response_handle, response, error);
153-
response_handle_free(response_handle);
159+
self->engine, owned_response_handle->response_handle, response, error);
154160

155161
return result;
156162
}

shell/platform/linux/public/flutter_linux/fl_binary_messenger.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ typedef struct _FlBinaryMessengerResponseHandle FlBinaryMessengerResponseHandle;
3939
* @messenger: a #FlBinaryMessenger
4040
* @channel: channel message received on
4141
* @message: message content received from Dart
42-
* @response_handle: a handle to respond to the message with
42+
* @response_handle: (transfer full): a handle to respond to the message with
4343
* @user_data: (closure): data provided when registering this callback
4444
*
4545
* Function called when platform messages are received. The receiver must
@@ -74,7 +74,8 @@ void fl_binary_messenger_set_message_handler_on_channel(
7474
/**
7575
* fl_binary_messenger_send_response:
7676
* @binary_messenger: a #FlBinaryMessenger
77-
* @response_handle: handle that was provided in a #FlBinaryMessengerCallback
77+
* @response_handle: (transfer full): handle that was provided in a
78+
* #FlBinaryMessengerCallback
7879
* @response: (allow-none): response to send or %NULL for an empty response
7980
* @error: (allow-none): #GError location to store the error occurring, or %NULL
8081
* to ignore

0 commit comments

Comments
 (0)