After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 782109 - wayland: memory leak when exporting handle
wayland: memory leak when exporting handle
Status: RESOLVED FIXED
Product: gtk+
Classification: Platform
Component: Backend: Wayland
3.22.x
Other Linux
: Normal normal
: ---
Assigned To: gtk-bugs
gtk-bugs
Depends on:
Blocks:
 
 
Reported: 2017-05-03 09:51 UTC by Timm Bäder
Modified: 2017-05-09 15:23 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
GdkWaylandWindow: Clear export user data when used (1.07 KB, patch)
2017-05-08 12:54 UTC, Jonas Ådahl
committed Details | Review
GdkWaylandWindow: Unexport when finalizing (1.72 KB, patch)
2017-05-08 12:54 UTC, Jonas Ådahl
committed Details | Review
GtkWindow: Don't double free export user data (1.20 KB, patch)
2017-05-09 15:19 UTC, Jonas Ådahl
committed Details | Review

Description Timm Bäder 2017-05-03 09:51:44 UTC
In 

https://git.gnome.org/browse/gtk+/tree/gdk/wayland/gdkwindow-wayland.c#n3962

the impl->exported.user_data is never freed, even though impl->exported even has a destroy_func member.
Comment 1 Matthias Clasen 2017-05-03 10:08:29 UTC
not the most obvious use, but here it is:

  g_clear_pointer (&impl->exported.user_data,
                   impl->exported.destroy_func);
Comment 2 Timm Bäder 2017-05-03 12:28:34 UTC
When running the org.gnome.PortalTest app in flatpak with -d --command=sh and thenin valgrind, I get

==5== 41 bytes in 1 blocks are definitely lost in loss record 7,880 of 14,775
==5==    at 0x4C28E0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5==    by 0x7050937: vasprintf (vasprintf.c:76)
==5==    by 0x6A281CD: g_vasprintf (gprintf.c:316)
==5==    by 0x6A0376F: g_strdup_vprintf (gstrfuncs.c:514)
==5==    by 0x6A0380B: g_strdup_printf (gstrfuncs.c:540)
==5==    by 0x40CE94: ??? (in /app/bin/portal-test)
==5==    by 0x57C09E4: xdg_exported_handle (gdkwindow-wayland.c:4025)
==5==    by 0xC1D4D03: ffi_call_unix64 (in /usr/lib/libffi.so.6.0.4)
==5==    by 0xC1D47F9: ffi_call (in /usr/lib/libffi.so.6.0.4)
==5==    by 0xA0D4AB9: wl_closure_invoke (connection.c:935)
==5==    by 0xA0D1A2F: dispatch_event.isra.4 (wayland-client.c:1310)
==5==    by 0xA0D2B6B: dispatch_queue (wayland-client.c:1456)
==5==    by 0xA0D2B6B: wl_display_dispatch_queue_pending (wayland-client.c:1698)
==5==    by 0x57BA4C3: _gdk_wayland_display_queue_events (gdkeventsource.c:198)
==5==    by 0x5763198: gdk_display_get_event (gdkdisplay.c:438)
==5==    by 0x57BA1B1: gdk_event_source_dispatch (gdkeventsource.c:117)
==5==    by 0x69E4536: g_main_dispatch (gmain.c:3203)
==5==    by 0x69E4536: g_main_context_dispatch (gmain.c:3856)
==5==    by 0x69E4767: g_main_context_iterate.isra.29 (gmain.c:3929)
==5==    by 0x69E480B: g_main_context_iteration (gmain.c:3990)
==5==    by 0x644CDEC: g_application_run (gapplication.c:2381)
==5==    by 0x40ABA1: ??? (in /app/bin/portal-test)
==5==    by 0x700468F: (below main) (libc-start.c:289)


when clicking the linkbutton. I also see a critical but I'm not sure if that's related.
Comment 3 Jonas Ådahl 2017-05-08 12:54:39 UTC
Created attachment 351347 [details] [review]
GdkWaylandWindow: Clear export user data when used

It was only cleared when unexported, but we could just as well clear it
when its used too.
Comment 4 Jonas Ådahl 2017-05-08 12:54:45 UTC
Created attachment 351348 [details] [review]
GdkWaylandWindow: Unexport when finalizing

The application might not have exported, and it'll be too late for it
at this point anyway, so lets be helpful and not leak it.
Comment 5 Matthias Clasen 2017-05-08 16:57:44 UTC
Review of attachment 351347 [details] [review]:

sure, ok
Comment 6 Matthias Clasen 2017-05-08 19:39:51 UTC
Review of attachment 351348 [details] [review]:

sure
Comment 7 Matthias Clasen 2017-05-08 19:42:23 UTC
Attachment 351347 [details] pushed as 6d77498 - GdkWaylandWindow: Clear export user data when used
Attachment 351348 [details] pushed as 251e216 - GdkWaylandWindow: Unexport when finalizing
Comment 8 Matthias Clasen 2017-05-08 20:01:47 UTC
Had to revert the second patch, since it doesn't build...
Comment 9 Jonas Ådahl 2017-05-08 23:58:16 UTC
(In reply to Matthias Clasen from comment #8)
> Had to revert the second patch, since it doesn't build...

Oops. The build fixes had "leaked" into the bug 782325 patches. I'll update both.
Comment 10 Jonas Ådahl 2017-05-09 15:19:27 UTC
Created attachment 351443 [details] [review]
GtkWindow: Don't double free export user data

The user data passed when exporting a Wayland window was supposed to be
freed using the destroy_func, as is commonly done. This was previously
broken, as the user data was just NULL:ed when exported, and only
actually destroyed when unexporting before having exported.

While e016d9a5dba6f6f99aee94d0b72c00bee299b96a fixed this, it introduced
a regression, as GtkWindow was nice enough to free the memory anyway
after having received the exported handle, causing it now to double
free.
Comment 11 Jonas Ådahl 2017-05-09 15:23:37 UTC
Comment on attachment 351443 [details] [review]
GtkWindow: Don't double free export user data

Attachment 351443 [details] pushed as d8bb385 - GtkWindow: Don't double free export user data