Skip to content

Commit a800957

Browse files
committed
Fix memory leaks
- Fix leaks to replace g_slist_remove_link() with g_slist_delete_link() in bus/dbusimpl.c and bus/matchrule.c - Fix D-Bus leaks after g_dbus_proxy_call() is called, to unrefer the referred GVariant in case it's floating in bus/engineproxy.c and bus/panelproxy.c - Fix several leaks in bus/ibusimpl.c: o Fix leaks of `names` in g_variant_get (params, "(^a&s)", &names) because each element in `names` is const but `names` should be freed in bus/ibusimpl.c o Fix leaks to replace g_ptr_array_free (array, FALSE) with g_ptr_array_free (array, TRUE) in bus/ibusimpl.c o Fix leaks of IBusRegistry to unrefer it before it's allocated again in bus/ibusimpl.c - Fix leaks of BusInputContext->queue_during_process_key_event in bus/inputcontext.c - Fix leaks of IBusImpl and DBusImpl when the ibus-daemon exits or restarts in bus/server.c - Fix several leaks in engine/main.vala: o Fix leak of IBus.Engine because casting IBus.Engine to IBus.EngineSimple causes a referring in Vala in engine/main.vala o Fix leak of IBus.Engine to assign null explicitly in engine/main.vala - Fix several leaks in src/emoji-parser.c o If the pointer of GSList exists, the updated GSList should not be replaced in IBusEmojiData in src/emoji-parser.c because the source and dest GSList have the shared elements in src/emoji-parser.c o Fix leaks to replace g_slist_free() with g_slist_free_full() in src/emoji-parser.c o Fix leaks to add a destroy function in src/emoji-parser.c o Delete g_set_prgname() as g_option_context_parse() calls it in src/emoji-parser.c - Fix leaks to call ibus_compose_table_free() in src/gencomposetable.c - Fix D-Bus leaks to unrefer GVariant in org.freedesktop.DBus.Properties.Set D-Bus method in src/ibusbus.c - Show stderr message for a forked progam from fanalyzer program in src/ibuscomponent.c - Fix leaks in the buffer to refer the middle pointer of the reallocated buffer instead of a new allocated buffer to free the buffer correctly in src/ibuscomposetable.c - Fix D-Bus leaks to unrefer the GObject if it's floating in src/ibusengine.c - Fix leaks of the pointer of g_resources_lookup_data() to call g_bytes_unref(), which does not free the internal data, in src/ibusenginesimple.c - Fix leaks in g_variant_get_data() to call g_dbus_method_invocation_return_value() in src/ibuspanelservice.c - Fix leaks of IBusXEvent->params in src/ibusxevent.c - Fix leaks of GError in case ibus_bus_set_global_engine() is failed in src/tests/ibus-bus.c - Fix leaks of GSList of gtk_window_list_toplevels() and add a destroy function in src/tests/ibus-compose.c - Fix leaks to add a destroy function in src/tests/ibus-keypress.c - Fix leaks of gchar of ibus_get_language_name() in src/tests/ibus-util.c - Fix leak of `struct udev_device` in src/tests/uinput-replay.c - Fix leak of XI.EventMask.mask in ui/gtk3/keybindingmanager.vala - Fix leak of m_status_icon in ui/gtk3/panel.vala - Fix leak of IBusXEvent since it's not floating in ui/gtk3/panelbinding.vala BUG=#2682
1 parent 0922e96 commit a800957

25 files changed

+332
-149
lines changed

bus/dbusimpl.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* vim:set et sts=4: */
33
/* ibus - The Input Bus
44
* Copyright (C) 2008-2013 Peng Huang <[email protected]>
5-
* Copyright (C) 2015-2024 Takao Fujiwara <[email protected]>
5+
* Copyright (C) 2015-2025 Takao Fujiwara <[email protected]>
66
* Copyright (C) 2008-2024 Red Hat, Inc.
77
*
88
* This library is free software; you can redistribute it and/or
@@ -489,7 +489,11 @@ bus_name_service_remove_owner (BusNameService *service,
489489
}
490490
}
491491

492-
service->owners = g_slist_remove_link (service->owners, (gpointer) owners);
492+
/* Should use g_slist_delete_link() instead of g_slist_remove_link(), to
493+
* delete the slice link in g_slist_prepend() in
494+
* bus_name_service_set_primary_owner().
495+
*/
496+
service->owners = g_slist_delete_link (service->owners, (gpointer) owners);
493497
}
494498

495499
static gboolean
@@ -661,8 +665,12 @@ bus_dbus_impl_destroy (BusDBusImpl *dbus)
661665
g_list_free (dbus->connections);
662666
dbus->connections = NULL;
663667

664-
g_hash_table_remove_all (dbus->unique_names);
665-
g_hash_table_remove_all (dbus->names);
668+
/* g_hash_table_destroy() calls both g_hash_table_remove_all() and
669+
* g_hash_table_unref() and the node destruction is called in
670+
* g_hash_table_remove_all_nodes().
671+
*/
672+
g_hash_table_destroy (dbus->unique_names);
673+
g_hash_table_destroy (dbus->names);
666674

667675
dbus->unique_names = NULL;
668676
dbus->names = NULL;

bus/engineproxy.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,6 +1690,9 @@ bus_engine_proxy_panel_extension_register_keys (BusEngineProxy *engine,
16901690
NULL,
16911691
NULL,
16921692
NULL);
1693+
if (!g_variant_is_floating (parameters)) {
1694+
g_variant_unref (parameters);
1695+
}
16931696
}
16941697

16951698
static gboolean

bus/ibusimpl.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ bus_ibus_impl_set_panel_extension_keys (BusIBusImpl *ibus,
382382

383383
if (ibus->extension_register_keys)
384384
g_variant_unref (ibus->extension_register_keys);
385-
ibus->extension_register_keys = g_variant_ref_sink (parameters);
385+
/* g_variant_ref_sink(parameters) has been called. */
386+
ibus->extension_register_keys = g_variant_ref (parameters);
386387
if (ibus->focused_context)
387388
engine = bus_input_context_get_engine (ibus->focused_context);
388389
if (!engine)
@@ -1504,6 +1505,7 @@ _ibus_get_engines_by_names (BusIBusImpl *ibus,
15041505
}
15051506
g_dbus_method_invocation_return_value (invocation,
15061507
g_variant_new ("(av)", &builder));
1508+
g_free (names);
15071509
}
15081510

15091511
/**
@@ -1918,7 +1920,7 @@ _ibus_set_preload_engines (BusIBusImpl *ibus,
19181920
for (i = 0; names[i] != NULL; i++) {
19191921
gboolean has_component = FALSE;
19201922

1921-
desc = bus_ibus_impl_get_engine_desc(ibus, names[i]);
1923+
desc = bus_ibus_impl_get_engine_desc (ibus, names[i]);
19221924

19231925
if (desc == NULL) {
19241926
g_set_error (error,
@@ -1927,6 +1929,7 @@ _ibus_set_preload_engines (BusIBusImpl *ibus,
19271929
"Cannot find engine %s.",
19281930
names[i]);
19291931
g_ptr_array_free (array, FALSE);
1932+
g_free (names);
19301933
return FALSE;
19311934
}
19321935

@@ -1948,13 +1951,14 @@ _ibus_set_preload_engines (BusIBusImpl *ibus,
19481951
g_ptr_array_add (array, component);
19491952
}
19501953
}
1954+
g_free (names);
19511955

19521956
for (j = 0; j < array->len; j++) {
19531957
bus_component_start ((BusComponent *) g_ptr_array_index (array, j),
19541958
g_verbose);
19551959
}
19561960

1957-
g_ptr_array_free (array, FALSE);
1961+
g_ptr_array_free (array, TRUE);
19581962

19591963
bus_ibus_impl_property_changed (ibus, "PreloadEngines", value);
19601964

@@ -2270,7 +2274,7 @@ bus_ibus_impl_registry_init (BusIBusImpl *ibus)
22702274
GList *components;
22712275
IBusRegistry *registry = ibus_registry_new ();
22722276

2273-
ibus->registry = NULL;
2277+
g_assert (!ibus->registry);
22742278
ibus->components = NULL;
22752279
ibus->engine_table = g_hash_table_new (g_str_hash, g_str_equal);
22762280

@@ -2291,12 +2295,14 @@ bus_ibus_impl_registry_init (BusIBusImpl *ibus)
22912295
ibus_registry_check_modification (registry)) {
22922296

22932297
ibus_object_destroy (IBUS_OBJECT (registry));
2298+
g_object_unref (registry);
22942299
registry = ibus_registry_new ();
22952300

22962301
if (ibus_registry_load_cache (registry, TRUE) == FALSE ||
22972302
ibus_registry_check_modification (registry)) {
22982303

22992304
ibus_object_destroy (IBUS_OBJECT (registry));
2305+
g_object_unref (registry);
23002306
registry = ibus_registry_new ();
23012307
ibus_registry_load (registry);
23022308
ibus_registry_save_cache (registry, TRUE);
@@ -2350,9 +2356,8 @@ bus_ibus_impl_registry_destroy (BusIBusImpl *ibus)
23502356

23512357
g_clear_pointer (&ibus->engine_table, g_hash_table_destroy);
23522358

2353-
/* g_clear_pointer() does not set the cast. */
23542359
ibus_object_destroy (IBUS_OBJECT (ibus->registry));
2355-
ibus->registry = NULL;
2360+
g_clear_object (&ibus->registry);
23562361

23572362
if (ibus->extension_register_keys)
23582363
g_clear_pointer (&ibus->extension_register_keys, g_variant_unref);

bus/inputcontext.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,14 @@ G_DEFINE_TYPE (BusInputContext, bus_input_context, IBUS_TYPE_SERVICE)
393393
&& !context->is_extension_lookup_table \
394394
&& bus_ibus_impl_is_wayland_session (BUS_DEFAULT_IBUS))
395395

396+
static void
397+
queue_process_key_event_free (gpointer user_data)
398+
{
399+
SyncForwardingData *data = (SyncForwardingData *)user_data;
400+
g_object_unref (data->text);
401+
g_slice_free (SyncForwardingData, data);
402+
}
403+
396404
static void
397405
_connection_destroy_cb (BusConnection *connection,
398406
BusInputContext *context)
@@ -846,6 +854,8 @@ bus_input_context_destroy (BusInputContext *context)
846854
context->client = NULL;
847855
}
848856

857+
g_queue_free_full (context->queue_during_process_key_event,
858+
queue_process_key_event_free);
849859
IBUS_OBJECT_CLASS (bus_input_context_parent_class)->
850860
destroy (IBUS_OBJECT (context));
851861
}

bus/matchrule.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* vim:set et sts=4: */
33
/* IBus - The Input Bus
44
* Copyright (C) 2008-2010 Peng Huang <[email protected]>
5-
* Copyright (C) 2024 Takao Fujiwara <[email protected]>
5+
* Copyright (C) 2024-2025 Takao Fujiwara <[email protected]>
66
* Copyright (C) 2008-2024 Red Hat, Inc.
77
*
88
* This library is free software; you can redistribute it and/or
@@ -654,7 +654,7 @@ bus_match_rule_connection_destroy_cb (BusConnection *connection,
654654
BusRecipient *recipient = (BusRecipient *)p->data;
655655

656656
if (recipient->connection == connection) {
657-
rule->recipients = g_list_remove_link (rule->recipients, p);
657+
rule->recipients = g_list_delete_link (rule->recipients, p);
658658
bus_recipient_free (recipient);
659659
return;
660660
}
@@ -703,10 +703,15 @@ bus_match_rule_remove_recipient (BusMatchRule *rule,
703703
BusRecipient *recipient = (BusRecipient *) p->data;
704704
if (connection == recipient->connection) {
705705
if (bus_recipient_unref (recipient)) {
706-
rule->recipients = g_list_remove_link (rule->recipients, p);
707-
g_signal_handlers_disconnect_by_func (connection,
708-
G_CALLBACK (bus_match_rule_connection_destroy_cb),
709-
rule);
706+
/* Use g_list_delete_link() instead of g_list_remove_link()
707+
* to delete the node slice in g_list_append() in
708+
* bus_match_rule_add_recipient().
709+
*/
710+
rule->recipients = g_list_delete_link (rule->recipients, p);
711+
g_signal_handlers_disconnect_by_func (
712+
connection,
713+
G_CALLBACK (bus_match_rule_connection_destroy_cb),
714+
rule);
710715
}
711716

712717
if (rule->recipients == NULL ) {

bus/panelproxy.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ static void
418418
bus_panel_proxy_g_signal (GDBusProxy *proxy,
419419
const gchar *sender_name,
420420
const gchar *signal_name,
421-
GVariant *parameters)
421+
GVariant *parameters) /* Expect ref_count is 3 */
422422
{
423423
BusPanelProxy *panel = (BusPanelProxy *)proxy;
424424

@@ -1202,4 +1202,7 @@ bus_panel_proxy_send_message_received (BusPanelProxy *panel,
12021202
g_variant_ref (parameters),
12031203
G_DBUS_CALL_FLAGS_NONE,
12041204
-1, NULL, NULL, NULL);
1205+
if (!g_variant_is_floating (parameters)) {
1206+
g_variant_unref (parameters);
1207+
}
12051208
}

bus/server.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* vim:set et sts=4: */
33
/* bus - The Input Bus
44
* Copyright (C) 2008-2010 Peng Huang <[email protected]>
5-
* Copyright (C) 2011-2022 Takao Fujiwara <[email protected]>
5+
* Copyright (C) 2011-2025 Takao Fujiwara <[email protected]>
66
* Copyright (C) 2008-2021 Red Hat, Inc.
77
*
88
* This library is free software; you can redistribute it and/or
@@ -367,6 +367,8 @@ bus_server_run (void)
367367

368368
ibus_object_destroy ((IBusObject *)dbus);
369369
ibus_object_destroy ((IBusObject *)ibus);
370+
g_object_unref (ibus);
371+
g_object_unref (dbus);
370372

371373
/* release resources */
372374
g_object_unref (server);

engine/main.vala

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* ibus - The Input Bus
44
*
55
* Copyright (c) 2011-2013 Peng Huang <[email protected]>
6-
* Copyright (c) 2015 Takao Fujiwara <[email protected]>
6+
* Copyright (c) 2015-2025 Takao Fujiwara <[email protected]>
77
*
88
* This library is free software; you can redistribute it and/or
99
* modify it under the terms of the GNU Lesser General Public
@@ -23,6 +23,8 @@
2323

2424
using IBus;
2525

26+
static IBus.EngineSimple m_simple;
27+
2628
class DummyEngine : IBus.EngineSimple {
2729
}
2830

@@ -54,6 +56,9 @@ public int main(string[] args) {
5456
IBus.Engine engine = new IBus.Engine.with_type(
5557
typeof(IBus.EngineSimple), name,
5658
path.printf(++id), bus.get_connection());
59+
/* the type change causes engine.ref(). */
60+
m_simple = (IBus.EngineSimple)engine;
61+
m_simple.unref();
5762

5863
/* Use Idle.add() to reduce the lag caused by file io */
5964
GLib.Idle.add(() => {
@@ -67,22 +72,21 @@ public int main(string[] args) {
6772
* In /usr/share/X11/locale/en_US.UTF-8/Compose ,
6873
* <Multi_key> <apostrophe> <c> : U0107
6974
*/
70-
IBus.EngineSimple? simple = (IBus.EngineSimple ?) engine;
71-
simple.add_table_by_locale(null);
75+
m_simple.add_table_by_locale(null);
7276

7377
string user_file = null;
7478

7579
var home = GLib.Environment.get_home_dir();
7680
if (home != null) {
7781
user_file = home + "/.XCompose";
7882
if (GLib.FileUtils.test(user_file, GLib.FileTest.EXISTS))
79-
simple.add_compose_file(user_file);
83+
m_simple.add_compose_file(user_file);
8084
}
8185

8286
user_file = GLib.Environment.get_variable("XCOMPOSEFILE");
8387
if (user_file != null) {
8488
if (GLib.FileUtils.test(user_file, GLib.FileTest.EXISTS))
85-
simple.add_compose_file(user_file);
89+
m_simple.add_compose_file(user_file);
8690
}
8791

8892
return false;
@@ -102,6 +106,7 @@ public int main(string[] args) {
102106
}
103107

104108
IBus.main();
109+
m_simple = null;
105110

106111
return 0;
107112
}

0 commit comments

Comments
 (0)