Skip to content

fix: prevent GBytes leak in GdkPixbufFromSkBitmap on Linux/GTK#49863

Merged
jkleinsc merged 1 commit into
electron:mainfrom
cinu:fix-gbytes-leak
Feb 20, 2026
Merged

fix: prevent GBytes leak in GdkPixbufFromSkBitmap on Linux/GTK#49863
jkleinsc merged 1 commit into
electron:mainfrom
cinu:fix-gbytes-leak

Conversation

@cinu
Copy link
Copy Markdown
Contributor

@cinu cinu commented Feb 19, 2026

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

Inside gtk_util::GdkPixbufFromSkBitmap, 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 x width x height bytes of pixel data on every
call.
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Feb 19, 2026
@codebytere codebytere requested a review from ckerr February 19, 2026 08:55
Copy link
Copy Markdown
Member

@ckerr ckerr left a comment

Choose a reason for hiding this comment

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

Oof, you're right. Thanks -- good catch!

@ckerr ckerr added semver/patch backwards-compatible bug fixes target/38-x-y PR should also be added to the "38-x-y" branch. target/39-x-y PR should also be added to the "39-x-y" branch. target/40-x-y PR should also be added to the "40-x-y" branch. target/41-x-y PR should also be added to the "41-x-y" branch. labels Feb 19, 2026
Copy link
Copy Markdown
Member

@deepak1556 deepak1556 left a comment

Choose a reason for hiding this comment

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

Thanks!

@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Feb 20, 2026
@jkleinsc jkleinsc merged commit 2b80f4c into electron:main Feb 20, 2026
145 of 147 checks passed
@welcome
Copy link
Copy Markdown

welcome Bot commented Feb 20, 2026

Congrats on merging your first pull request! 🎉🎉🎉

@release-clerk
Copy link
Copy Markdown

release-clerk Bot commented Feb 20, 2026

Release Notes Persisted

Fix memory leak when setting icons on Linux/GTK

@trop
Copy link
Copy Markdown
Contributor

trop Bot commented Feb 20, 2026

I have automatically backported this PR to "41-x-y", please check out #49895

@trop
Copy link
Copy Markdown
Contributor

trop Bot commented Feb 20, 2026

I have automatically backported this PR to "39-x-y", please check out #49896

@trop
Copy link
Copy Markdown
Contributor

trop Bot commented Feb 20, 2026

I have automatically backported this PR to "38-x-y", please check out #49897

@trop trop Bot added in-flight/39-x-y and removed target/41-x-y PR should also be added to the "41-x-y" branch. labels Feb 20, 2026
@trop
Copy link
Copy Markdown
Contributor

trop Bot commented Feb 20, 2026

I have automatically backported this PR to "40-x-y", please check out #49898

@trop trop Bot added in-flight/38-x-y in-flight/40-x-y and removed target/39-x-y PR should also be added to the "39-x-y" branch. target/38-x-y PR should also be added to the "38-x-y" branch. labels Feb 20, 2026
@trop trop Bot added merged/39-x-y PR was merged to the "39-x-y" branch. merged/41-x-y PR was merged to the "41-x-y" branch. merged/40-x-y PR was merged to the "40-x-y" branch. merged/38-x-y PR was merged to the "38-x-y" branch. and removed target/40-x-y PR should also be added to the "40-x-y" branch. in-flight/39-x-y in-flight/41-x-y in-flight/40-x-y in-flight/38-x-y labels Feb 20, 2026
kycutler pushed a commit to kycutler/electron that referenced this pull request Feb 26, 2026
…ron#49863)

Inside gtk_util::GdkPixbufFromSkBitmap, 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 x width x height bytes of pixel data on every
call.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged/38-x-y PR was merged to the "38-x-y" branch. merged/39-x-y PR was merged to the "39-x-y" branch. merged/40-x-y PR was merged to the "40-x-y" branch. merged/41-x-y PR was merged to the "41-x-y" branch. semver/patch backwards-compatible bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants