#49863: fix: prevent GBytes leak in GdkPixbufFromSkBitmap on Linux/GTK
Description of Change
g_bytes_new() was called inline as an argument to gdk_pixbuf_new_from_bytes(), which per GTK docs does not take ownership of the GBytes - it adds its own internal reference. The caller's GBytes* was never stored or unreffed, leaking 4 * width * height bytes of pixel data on every call.
This is most impactful when calling Tray.setImage() repeatedly (e.g., for animated tray icons), causing continuously growing process memory that is never reclaimed.
Internally, gdk_pixbuf_new_from_bytes() calls g_object_new() with the "pixel-bytes" construct property. In gdk_pixbuf_set_property(), the PROP_PIXEL_BYTES case stores the bytes via g_value_dup_boxed(), which calls g_boxed_copy() -> _g_type_boxed_copy() -> the registered copy function. Since G_TYPE_BYTES is registered as G_DEFINE_BOXED_TYPE(GBytes, g_bytes, g_bytes_ref, g_bytes_unref), the copy function is g_bytes_ref(), incrementing the refcount.
Simple app that sets tray icon (1024x1024) 100 times:
Before fix: RSS 633.07 MB
After fix: RSS 282.91 MB
Checklist
- PR description included
- I have built and tested this PR
Release Notes
Notes: Fix memory leak when setting icons on Linux/GTK
Backports
Semver Impact
Semantic Versioning helps users understand the impact of updates:
- Major (X.y.z): Breaking changes that may require code modifications
- Minor (x.Y.z): New features that maintain backward compatibility
- Patch (x.y.Z): Bug fixes that don't change the API
- None: Changes that don't affect using facing parts of Electron