diff --git a/shell/platform/linux/fl_binary_messenger.cc b/shell/platform/linux/fl_binary_messenger.cc index 40fd541b9d37c..fe67cb4589d85 100644 --- a/shell/platform/linux/fl_binary_messenger.cc +++ b/shell/platform/linux/fl_binary_messenger.cc @@ -45,11 +45,6 @@ struct _FlBinaryMessengerResponseHandle { const FlutterPlatformMessageResponseHandle* response_handle; }; -static void engine_weak_notify_cb(gpointer user_data, GObject* object) { - FlBinaryMessenger* self = FL_BINARY_MESSENGER(user_data); - self->engine = nullptr; -} - static FlBinaryMessengerResponseHandle* response_handle_new( const FlutterPlatformMessageResponseHandle* response_handle) { FlBinaryMessengerResponseHandle* handle = @@ -64,6 +59,14 @@ static void response_handle_free(FlBinaryMessengerResponseHandle* handle) { g_free(handle); } +G_DEFINE_AUTOPTR_CLEANUP_FUNC(FlBinaryMessengerResponseHandle, + response_handle_free); + +static void engine_weak_notify_cb(gpointer user_data, GObject* object) { + FlBinaryMessenger* self = FL_BINARY_MESSENGER(user_data); + self->engine = nullptr; +} + static gboolean fl_binary_messenger_platform_message_callback( FlEngine* engine, const gchar* channel, @@ -145,12 +148,15 @@ G_MODULE_EXPORT gboolean fl_binary_messenger_send_response( g_return_val_if_fail(FL_IS_BINARY_MESSENGER(self), FALSE); g_return_val_if_fail(response_handle != nullptr, FALSE); + // Take reference to ensure it is freed + g_autoptr(FlBinaryMessengerResponseHandle) owned_response_handle = + response_handle; + if (self->engine == nullptr) return TRUE; gboolean result = fl_engine_send_platform_message_response( - self->engine, response_handle->response_handle, response, error); - response_handle_free(response_handle); + self->engine, owned_response_handle->response_handle, response, error); return result; } diff --git a/shell/platform/linux/public/flutter_linux/fl_binary_messenger.h b/shell/platform/linux/public/flutter_linux/fl_binary_messenger.h index f37aff43095df..32a93ec041e55 100644 --- a/shell/platform/linux/public/flutter_linux/fl_binary_messenger.h +++ b/shell/platform/linux/public/flutter_linux/fl_binary_messenger.h @@ -39,7 +39,7 @@ typedef struct _FlBinaryMessengerResponseHandle FlBinaryMessengerResponseHandle; * @messenger: a #FlBinaryMessenger * @channel: channel message received on * @message: message content received from Dart - * @response_handle: a handle to respond to the message with + * @response_handle: (transfer full): a handle to respond to the message with * @user_data: (closure): data provided when registering this callback * * Function called when platform messages are received. The receiver must @@ -74,7 +74,8 @@ void fl_binary_messenger_set_message_handler_on_channel( /** * fl_binary_messenger_send_response: * @binary_messenger: a #FlBinaryMessenger - * @response_handle: handle that was provided in a #FlBinaryMessengerCallback + * @response_handle: (transfer full): handle that was provided in a + * #FlBinaryMessengerCallback * @response: (allow-none): response to send or %NULL for an empty response * @error: (allow-none): #GError location to store the error occurring, or %NULL * to ignore