Subject: Re: bug#70352: Fwd: Re: bug#70352: 29.3.50; emacs-pgtk: possible leak of virtual memory

From: Yang Yingchao <[email-masked]>
Date: Mon, 29 Apr 2024 16:45:16 +0800

On Sat, Apr 27 2024, Eli Zaretskii wrote:

>> Date: Wed, 17 Apr 2024 15:30:11 +0800
>> From:  Yang Yingchao via "Bug reports for GNU Emacs,
>>  the Swiss army knife of text editors" <[email-masked]>
>> 
>> On Mon, Apr 15 2024, Yang Yingchao wrote:
>> 
>> >
>> > FYI:
>> > I tested this in two window managers (Hyprland & Sway), and found that the same issue exists.
>> 
>> 
>> I'm not familiar with the GTK toolkit, but it seems that the creation
>> and destruction of surfaces are not properly paired.
>> 
>> 
>> I added breakpoints to `_gdk_wayland_display_create_shm_surface()` and
>> `gdk_wayland_cairo_surface_destroy()`, then executed steps 2 and 3. I
>> found that `_gdk_wayland_display_create_shm_surface()` is called 3
>> times, but `gdk_wayland_cairo_surface_destroy()` is only called 2 times.
>> 
>> I'm not sure if this is related...
>> 
>> ```
>> 70:Thread 1 "emacs" hit Breakpoint 6, _gdk_wayland_display_create_shm_surface (display=<optimized out>,
>> 125:Thread 1 "emacs" hit Breakpoint 5, gdk_wayland_cairo_surface_destroy (p=0x555b2d87c2a0)
>> 137:Thread 1 "emacs" hit Breakpoint 6, _gdk_wayland_display_create_shm_surface (display=<optimized out>,
>> 153:Thread 1 "emacs" hit Breakpoint 6, _gdk_wayland_display_create_shm_surface (display=<optimized out>,
>> 181:Thread 1 "emacs" hit Breakpoint 5, gdk_wayland_cairo_surface_destroy (p=0x555b2e33fc40)
>> ```
>
> Po Lu, any comments or suggestions?

Could this be an issue with gtk+? I made some changes to gtk+-3.24.41, and it seems like the issue has disappeared...

,----
| diff -urNa gtk+-3.24.41.orig/gdk/wayland/gdkwindow-wayland.c gtk+-3.24.41/gdk/wayland/gdkwindow-wayland.c
| --- gtk+-3.24.41.orig/gdk/wayland/gdkwindow-wayland.c	2024-01-24 09:14:34.000000000 +0800
| +++ gtk+-3.24.41/gdk/wayland/gdkwindow-wayland.c	2024-04-29 16:41:00.691373426 +0800
| @@ -952,6 +952,11 @@
|    /* Release came in, we haven't done any interim updates, so we can just use
|     * the old committed buffer again.
|     */
| +
| +  if (impl->staging_cairo_surface) {
| +    g_clear_pointer (&impl->staging_cairo_surface, cairo_surface_destroy);
| +  }
| +
|    impl->staging_cairo_surface = g_steal_pointer (&impl->committed_cairo_surface);
|  }
`----

Actually, I do not understand the logic of the function `buffer_release_callback()', but when
debugging this issue with gdb, I noticed that the `impl->staging_cairo_surface' which was created via
`_gdk_wayland_display_create_shm_surface' was replaced by `impl->committed_cairo_surface' without
being released first.

Regards,

-- *Yang Yingchao*
diff -urNa gtk+-3.24.41.orig/gdk/wayland/gdkwindow-wayland.c gtk+-3.24.41/gdk/wayland/gdkwindow-wayland.c
--- gtk+-3.24.41.orig/gdk/wayland/gdkwindow-wayland.c	2024-01-24 09:14:34.000000000 +0800
+++ gtk+-3.24.41/gdk/wayland/gdkwindow-wayland.c	2024-04-29 16:41:00.691373426 +0800
@@ -952,6 +952,11 @@
   /* Release came in, we haven't done any interim updates, so we can just use
    * the old committed buffer again.
    */
+
+  if (impl->staging_cairo_surface) {
+    g_clear_pointer (&impl->staging_cairo_surface, cairo_surface_destroy);
+  }
+
   impl->staging_cairo_surface = g_steal_pointer (&impl->committed_cairo_surface);
 }