Skip to content

pasteboard: free native memory in the Linux implementation - #538

Merged
boyan01 merged 2 commits into
mainfrom
pasteboard/free-linux-native-memory
Nov 9, 2026
Merged

boyan01 merged 2 commits into
mainfrom
pasteboard/free-linux-native-memory

Conversation

@YeungKC

@YeungKC YeungKC commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Summary

On Linux, Pasteboard.image did not free the PNG buffer from gdk_pixbuf_save_to_buffer. It also did not release the FlValue that holds a copy of that buffer, because fl_method_call_respond_success does not take ownership of the value. Each read leaked about two times the PNG size.

The same file has smaller leaks of the same type:

  • Pasteboard.files: the path from g_file_get_path and the string FlValue passed to fl_value_append.
  • Pasteboard.writeFiles: the clear callback freed only the array with delete[], not the g_strconcat strings. The array now comes from g_new and the callback frees it with g_strfreev.
  • The GError from gdk_pixbuf_save_to_buffer.
-  gchar *buffer = nullptr;
+  g_autofree gchar *buffer = nullptr;
   gsize buffer_size = 0;
-  GError *error = nullptr;
+  g_autoptr(GError) error = nullptr;
 ...
-  fl_method_call_respond_success(method_call,
-                                 fl_value_new_uint8_list(reinterpret_cast<const uint8_t *>(buffer), buffer_size),
-                                 nullptr);
+  g_autoptr(FlValue) result =
+      fl_value_new_uint8_list(reinterpret_cast<const uint8_t *>(buffer), buffer_size);
+  fl_method_call_respond_success(method_call, result, nullptr);

Evidence

Release build of the example on Linux (sway, Wayland, GTK 3.24.52). The app puts a 1024x1024 image on the clipboard (PNG 3.1 MB), reads Pasteboard.image 10 times to warm up, then reads it 200 times and compares RSS. The test code is not part of this change. The image is set from the same process. Three runs each:

  • Before: RSS +1394 MB, +1393 MB, +1394 MB (about 7 MB for each read)
    After: RSS +2 MB, +4 MB, +0 MB

writeFiles followed by files 200 times returns the correct paths each time after the change.

Merge Danger

Door: two-way

Blast Radius: small

Linux only. No API change. Each value is freed after the last use, with the GLib ownership rules for each call.

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The scoped ownership fixes preserve value lifetimes and use matching cleanup mechanisms, with no blocking issues identified.

No open issues 🎉

What changed in this PR

Fixes native memory leaks in the Linux pasteboard implementation without changing its public API.

Changes:

  • Frees image buffers, errors, and temporary values after use.
  • Releases file paths and clipboard URI arrays with matching ownership rules.
File Description
packages/​pasteboard/​linux/​pasteboard_plugin.cc Corrects memory cleanup for image reads and file clipboard operations.

Review effort: Balanced

@boyan01
boyan01 merged commit 9b9aa00 into main Nov 9, 2026
7 checks passed
@boyan01
boyan01 deleted the pasteboard/free-linux-native-memory branch November 9, 2026 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants