Public bug reported:

[ Impact ]

GtkGLArea leaks memory each time the widget is unrealized, for example when it
is removed from its parent and added back, or when its window or tab closes.
The memory is never returned to the system.

Every GTK 4 application that creates and destroys GLArea widgets is affected.
The Ghostty terminal (in universe) is a visible case: each terminal split, tab
and window is a GLArea. On my 26.04 laptop, a Ghostty instance that had opened
and closed many splits over a day used 468 MB PSS while showing only 2
terminals. The only workaround is to restart the application.

Upstream fixed this in GTK 4.23.3 with a small change to
gtk/gtkglarea.c:

  Issue:  https://gitlab.gnome.org/GNOME/gtk/-/work_items/8303
  MR:     https://gitlab.gnome.org/GNOME/gtk/-/merge_requests/10130
  Commit: "glarea: Fix memory leaks" (Matthias Clasen), 1 file, +7 / -2

The MR carries the upstream "Backport" label, but the fix is not in any
4.22.x release (the gtk-4-22 NEWS up to 4.22.5 does not list #8303).
Ubuntu 26.10 (stonking) already ships 4.24.1, which contains the fix.

The patch applies cleanly on top of the Ubuntu 4.22.4+ds-0ubuntu0.1 source
(2 hunks offset by 1 line). gtk/gtkglarea.c is unchanged between 4.22.4 and 
4.22.5.

[ Test Plan ]

1. Build the reproducer. It is the program from upstream issue #8303, with
   the button replaced by a 50 ms timer, so no clicking is needed. It does
   600 remove/re-add cycles and prints its own PSS every 100 cycles
   (attached as glarealeak-auto.c):
     gcc -o glarealeak glarealeak-auto.c $(pkg-config --cflags --libs gtk4 
epoxy)
2. Run ./glarealeak and compare the first and last PSS lines.
   Unfixed: PSS grows by roughly 1-4 MB per cycle and never drops.
   Fixed: PSS stays flat after the first cycles.
3. Regression check: run a few GLArea users (gtk4-demo "OpenGL Area",
   Ghostty, or another GL-backed GTK 4 app). Open and close windows, tabs
   and splits. Make sure rendering is correct and nothing crashes.

Results on my machine (600 cycles, PSS in MB):

  Library                                        start    end
  system 4.22.4+ds-0ubuntu0.1 (Vulkan renderer)     44    649
  system 4.22.4+ds-0ubuntu0.1, GSK_RENDERER=ngl     54   2432
  Ubuntu 4.22.4 source, rebuilt, unpatched         134    732
  Ubuntu 4.22.4 source, rebuilt, + upstream fix     52     53
  same, + upstream fix, GSK_RENDERER=ngl            52     55

The rebuilt libraries came from "apt source gtk4" (4.22.4+ds-0ubuntu0.1,
with all Ubuntu patches) and were loaded through LD_LIBRARY_PATH. They were
built with meson -Dvulkan=disabled, because glslc is not packaged.

[ Where problems could occur ]

The patch changes the reference handling of the textures that GLArea passes
to GSK (the gl_texture and the dmabuf_texture). A mistake there could cause
a use-after-free or a double free when a GLArea is unrealized while a
dmabuf texture is still in use by the renderer. The symptom would be a
crash, or a blank or corrupted GLArea, in GL-based GTK 4 applications,
most likely when a window or tab with GL content closes. The upstream
change has shipped in 4.23.3, 4.23.4, 4.24.0 and 4.24.1 since 2026-08-02,
and no later GLArea fix appears in the upstream NEWS.

[ Other Info ]

- System: Ubuntu 26.04.1, amd64, AMD Radeon (amdgpu), Wayland.
- Installed: libgtk-4-1 4.22.4+ds-0ubuntu0.1.
- The current proposed upload 4.22.4+ds-0ubuntu0.2 does not include this fix.
  It could ride along with that SRU or the next one.
- Related downstream report: https://github.com/ghostty-org/ghostty/issues/5491

** Affects: gtk4 (Ubuntu)
     Importance: Undecided
         Status: New


** Tags: resolute

** Attachment added: "Reproducer: upstream #8303 program driven by a 50 ms 
timer, 600 cycles, prints own PSS"
   
https://bugs.launchpad.net/bugs/2169936/+attachment/6007077/+files/glarealeak-auto.c

-- 
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.
https://bugs.launchpad.net/bugs/2169936

Title:
  [SRU] GtkGLArea leaks memory on every unrealize (upstream #8303, fixed
  in 4.23.3)

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/gtk4/+bug/2169936/+subscriptions


-- 
ubuntu-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to