GdkClipboard leak when using set_content() before read_text_async()
Steps to reproduce
- Call
gdk_clipboard_set_content()
(with text content) - Call
gdk_clipboard_read_text_async()
& finish
Current behavior
When calling gdk_clipboard_set_content()
before calling gdk_clipboard_read_text_async()
, LeakSanitizer reports a leaked GObject. If using gdk_clipboard_set_text()
there is no leak, and the leak also doesn't seem to occur until gdk_clipboard_read_text_async()
is called.
Expected outcome
No leak when using gdk_clipboard_set_content()
for text.
Version information
- gtk4-4.6.6-1.fc36
- Fedora Silverblue 36 (fedora 36 toolbox)
Additional information
I've tested this both with xvfb-run
and on my Wayland system, so I don't believe this is in a backend implementation. Valgrind reported no leaks for me. Possibly related to #4892 (closed).
Here is a copy-paste reproducer:
#include <gtk/gtk.h>
static void
read_text_cb (GdkClipboard *clipboard,
GAsyncResult *result,
char **text)
{
*text = gdk_clipboard_read_text_finish (clipboard, result, NULL);
}
static void
test_gdk_clipboard (void)
{
GdkClipboard *clipboard;
GdkContentProvider *content;
GBytes *bytes = NULL;
char *text = NULL;
clipboard = gdk_display_get_clipboard (gdk_display_get_default ());
bytes = g_bytes_new ("foobar", strlen ("foobar") + 1);
content = gdk_content_provider_new_for_bytes ("text/plain;charset=utf-8",
bytes);
gdk_clipboard_set_content (clipboard, content);
g_clear_pointer (&bytes, g_bytes_unref);
g_clear_object (&content);
gdk_clipboard_read_text_async (clipboard,
NULL,
(GAsyncReadyCallback)read_text_cb,
&text);
while (text == NULL)
g_main_context_iteration (NULL, FALSE);
g_assert_cmpstr ("foobar", ==, text);
g_clear_pointer (&text, g_free);
}
int
main (int argc,
char *argv[])
{
gtk_test_init (&argc, &argv, NULL);
g_test_add_func ("/gdk/clipboard", test_gdk_clipboard);
return g_test_run ();
}
And the output from LeakSanitizer:
=================================================================
==1239479==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 56 byte(s) in 1 object(s) allocated from:
#0 0x7fa29a2ba68f in __interceptor_malloc (/lib64/libasan.so.8+0xba68f)
#1 0x7fa29991e278 in g_malloc (/lib64/libglib-2.0.so.0+0x5a278)
#2 0x7fa299936ba5 in g_slice_alloc (/lib64/libglib-2.0.so.0+0x72ba5)
#3 0x7fa299dab466 (/lib64/libgtk-4.so.1+0x3ab466)
#4 0x7fa29a1dcfd8 in g_type_create_instance ../gobject/gtype.c:1929
#5 0x7fa29a1c4e37 in g_object_new_internal ../gobject/gobject.c:2011
#6 0x7fa29a1c6080 in g_object_new_with_properties ../gobject/gobject.c:2181
#7 0x7fa29a1c6b20 in g_object_new (/lib64/libgobject-2.0.so.0+0x25b20)
#8 0x7fa299d9439c (/lib64/libgtk-4.so.1+0x39439c)
#9 0x7fa299d95468 (/lib64/libgtk-4.so.1+0x395468)
#10 0x7fa299d957fe in gdk_clipboard_read_text_async (/lib64/libgtk-4.so.1+0x3957fe)
#11 0x401561 in test_gdk_clipboard ../../../src/tests/plugins/gtk/test-gdkclipboard.c:28
#12 0x7fa2999423dd in test_case_run ../glib/gtestutils.c:2930
#13 0x7fa2999423dd in g_test_run_suite_internal ../glib/gtestutils.c:3018
#14 0x7fa299942144 in g_test_run_suite_internal ../glib/gtestutils.c:3035
#15 0x7fa2999428e1 in g_test_run_suite ../glib/gtestutils.c:3112
#16 0x7fa29994294c in g_test_run (/lib64/libglib-2.0.so.0+0x7e94c)
#17 0x401268 in main ../../../src/tests/plugins/gtk/test-gdkclipboard.c:48
#18 0x7fa298e2954f in __libc_start_call_main (/lib64/libc.so.6+0x2954f)
#19 0x7fa298e29608 in __libc_start_main@@GLIBC_2.34 (/lib64/libc.so.6+0x29608)
#20 0x401344 in _start (/var/home/andrew/Projects/valent/_build_ci/test/asan/src/tests/plugins/gtk/test-gdkclipboard+0x401344)
-----------------------------------------------------
Suppressions used:
count bytes template
2 149 gtk_init
-----------------------------------------------------
SUMMARY: AddressSanitizer: 56 byte(s) leaked in 1 allocation(s).
------------------------------------------------------------------------------
Edited by Andy Holmes