From 2e6be1b5ae9174fa82281a1f8125137c31e3bb2e Mon Sep 17 00:00:00 2001 From: YeungKC <11473691+YeungKC@users.noreply.github.com> Date: Sun, 18 Oct 2026 15:09:51 +0900 Subject: [PATCH] pasteboard: free native memory in the Linux implementation Pasteboard.image did not free the PNG buffer from gdk_pixbuf_save_to_buffer or the FlValue that holds the copy, so each read leaked about two times the image size. Also free the GError, the file paths and their FlValue in Pasteboard.files, and the URI strings when the clipboard clears the data from Pasteboard.writeFiles. --- .../pasteboard/linux/pasteboard_plugin.cc | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/packages/pasteboard/linux/pasteboard_plugin.cc b/packages/pasteboard/linux/pasteboard_plugin.cc index 9837296f..a5f06520 100644 --- a/packages/pasteboard/linux/pasteboard_plugin.cc +++ b/packages/pasteboard/linux/pasteboard_plugin.cc @@ -28,9 +28,9 @@ static void gtk_clipboard_request_uris_callback( g_autoptr(FlValue) result = fl_value_new_list(); for (auto uri = uris; uri != nullptr && *uri != nullptr; uri++) { g_autoptr(GFile) file = g_file_new_for_uri(*uri); - auto *file_path = g_file_get_path(file); + g_autofree gchar *file_path = g_file_get_path(file); if (file_path) { - fl_value_append(result, fl_value_new_string(file_path)); + fl_value_append_take(result, fl_value_new_string(file_path)); } } fl_method_call_respond_success(method_call, result, nullptr); @@ -70,8 +70,7 @@ static void gtk_clipboard_get_file_uri(GtkClipboard *clipboard, static void gtk_clipboard_clear(GtkClipboard *clipboard, gpointer user_data_or_owner) { - auto **uris = reinterpret_cast(user_data_or_owner); - delete[] uris; + g_strfreev(reinterpret_cast(user_data_or_owner)); } static void clipboard_request_image_callback( @@ -85,9 +84,9 @@ static void clipboard_request_image_callback( return; } - gchar *buffer = nullptr; + g_autofree gchar *buffer = nullptr; gsize buffer_size = 0; - GError *error = nullptr; + g_autoptr(GError) error = nullptr; gdk_pixbuf_save_to_buffer(pixbuf, &buffer, &buffer_size, "png", &error, nullptr); if (error) { @@ -100,9 +99,9 @@ static void clipboard_request_image_callback( return; } - fl_method_call_respond_success(method_call, - fl_value_new_uint8_list(reinterpret_cast(buffer), buffer_size), - nullptr); + g_autoptr(FlValue) result = + fl_value_new_uint8_list(reinterpret_cast(buffer), buffer_size); + fl_method_call_respond_success(method_call, result, nullptr); } @@ -120,7 +119,7 @@ static void pasteboard_plugin_handle_method_call( auto args = fl_method_call_get_args(method_call); auto size = fl_value_get_length(args); - auto **paths = new gchar *[size + 1]; + auto **paths = g_new(gchar *, size + 1); paths[size] = nullptr; for (unsigned int i = 0; i < size; ++i) { auto path = fl_value_get_string(fl_value_get_list_value(args, i));