Skip to content

Commit 46f1427

Browse files
committed
gtk.cfg: g_idle_add and g_timeout_add take the data pointer
The data given to g_idle_add(), g_timeout_add() and their _seconds/_full variants is passed to the callback (and the destroy notify), which usually frees it. With <leak-ignore/> every such call was reported as Memory leak: data Configure them as <use> instead.
1 parent bf89a6f commit 46f1427

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

‎cfg/gtk.cfg‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,12 @@
717717
<alloc init="true" no-fail="true">g_variant_type_dup_string</alloc>
718718
<alloc init="true" no-fail="true">g_value_dup_string</alloc>
719719
<use>g_register_data</use>
720+
<use>g_idle_add</use>
721+
<use>g_idle_add_full</use>
722+
<use>g_timeout_add</use>
723+
<use>g_timeout_add_full</use>
724+
<use>g_timeout_add_seconds</use>
725+
<use>g_timeout_add_seconds_full</use>
720726
<dealloc>g_free</dealloc>
721727
</memory>
722728
<memory>
@@ -4237,7 +4243,6 @@
42374243
</function>
42384244
<!--guint g_idle_add (GSourceFunc function, gpointer data); -->
42394245
<function name="g_idle_add">
4240-
<leak-ignore/>
42414246
<noreturn>false</noreturn>
42424247
<returnValue type="guint"/>
42434248
<arg nr="1" direction="in">
@@ -4249,7 +4254,6 @@
42494254
</function>
42504255
<!--guint g_idle_add_full (gint priority, GSourceFunc function, gpointer data, GDestroyNotify notify); -->
42514256
<function name="g_idle_add_full">
4252-
<leak-ignore/>
42534257
<noreturn>false</noreturn>
42544258
<returnValue type="guint"/>
42554259
<arg nr="1">
@@ -5652,7 +5656,6 @@
56525656
<!-- guint g_timeout_add ( guint interval, GSourceFunc function, gpointer data) -->
56535657
<!-- guint g_timeout_add_seconds ( guint interval, GSourceFunc function, gpointer data) -->
56545658
<function name="g_timeout_add,g_timeout_add_seconds">
5655-
<leak-ignore/>
56565659
<noreturn>false</noreturn>
56575660
<returnValue type="guint"/>
56585661
<arg nr="1" direction="in">
@@ -5669,7 +5672,6 @@
56695672
</function>
56705673
<!-- guint g_timeout_add_full (gint priority, guint interval, GSourceFunc function, gpointer data, GDestroyNotify notify) -->
56715674
<function name="g_timeout_add_full,g_timeout_add_seconds_full">
5672-
<leak-ignore/>
56735675
<noreturn>false</noreturn>
56745676
<returnValue type="guint"/>
56755677
<arg nr="1" direction="in">

‎test/cfg/gtk.c‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,3 +730,45 @@ int g_define_enum_and_flags_type_test(void) {
730730
return e + f;
731731
}
732732
#endif
733+
734+
static gboolean source_func_frees_data(gpointer data)
735+
{
736+
g_free(data);
737+
return FALSE;
738+
}
739+
740+
static void destroy_notify_frees_data(gpointer data)
741+
{
742+
g_free(data);
743+
}
744+
745+
// The data passed to a GSource is owned by the callback or the destroy notify
746+
// from then on, so it does not leak.
747+
void g_idle_add_takes_data_test(void)
748+
{
749+
// cppcheck-suppress constVariablePointer
750+
gchar *data = g_strdup("data");
751+
g_idle_add(source_func_frees_data, data);
752+
753+
gchar *data_full = g_strdup("data");
754+
g_idle_add_full(200, source_func_frees_data, data_full, NULL);
755+
}
756+
757+
void g_timeout_add_takes_data_test(void)
758+
{
759+
// cppcheck-suppress constVariablePointer
760+
gchar *data = g_strdup("data");
761+
g_timeout_add(10, source_func_frees_data, data);
762+
763+
// cppcheck-suppress constVariablePointer
764+
gchar *data_seconds = g_strdup("data");
765+
g_timeout_add_seconds(1, source_func_frees_data, data_seconds);
766+
767+
// cppcheck-suppress constVariablePointer
768+
gchar *data_full = g_strdup("data");
769+
g_timeout_add_full(0, 10, source_func_frees_data, data_full, destroy_notify_frees_data);
770+
771+
// cppcheck-suppress constVariablePointer
772+
gchar *data_seconds_full = g_strdup("data");
773+
g_timeout_add_seconds_full(0, 1, source_func_frees_data, data_seconds_full, destroy_notify_frees_data);
774+
}

0 commit comments

Comments
 (0)