Memory leak using g_dbus_proxy_new_for_bus_sync

Hello,

When I wrote a test program, I find the memory is increasing……But I don’t know what is the wrong?
the program is call DBus method of NetworkManager by using Glib-Dbus.

int main()
{
	while(1)
	{
		GDBusProxy *proxy = NULL;
		GError *error = NULL;

		proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
			       G_DBUS_PROXY_FLAGS_NONE,
			       NULL,
			   NM_DBUS_SERVICE,	// "org.freedesktop.NetworkManager"
			       NM_DBUS_PATH,	//"/org/freedesktop/NetworkManager"
			       "org.freedesktop.DBus.Properties",
			       NULL, NULL);

		g_assert (proxy != NULL);

		GVariant *ret = NULL;
		ret = g_dbus_proxy_call_sync (proxy,
			  "Get",
			  g_variant_new("(ss)","org.freedesktop.NetworkManager","AllDevices"),
			  G_DBUS_CALL_FLAGS_NONE, -1,NULL, &error);

		if (!ret) 
		{
			g_dbus_error_strip_remote_error (error);
			g_print ("get failed: %s\n", error->message);
			g_error_free (error);
			g_object_unref (proxy);
			return -1;
		}
		g_variant_unref(ret);
		g_object_unref (proxy);
		usleep(1000);
	}
	return 0;
}

}AY~O))W7Y}507EZ3MZFF

I use valgrind to check it, the LEAK SUMMARY show definitely lost: 0 bytes in 0 blocks.

==2380== 919,584 bytes in 9,579 blocks are still reachable in loss record 1,159 of 1,160
==2380== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2380== by 0x4E898E0: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.2)
==2380== by 0x4E81CF9: g_source_new (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.2)
==2380== by 0x4E85071: g_idle_source_new (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.2)
==2380== by 0x521B9C5: ??? (in /usr/lib/x86_64-linux-gnu/libgio-2.0.so.0.4800.2)
==2380== by 0x52238D1: g_dbus_connection_signal_unsubscribe (in /usr/lib/x86_64-linux-gnu/libgio-2.0.so.0.4800.2)
==2380== by 0x522D3D8: ??? (in /usr/lib/x86_64-linux-gnu/libgio-2.0.so.0.4800.2)
==2380== by 0x54E7D49: g_object_unref (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.2)
==2380== by 0x400B70: main (b.c:38)
==2380==
==2380== 919,584 bytes in 9,579 blocks are still reachable in loss record 1,160 of 1,160
==2380== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2380== by 0x4E898E0: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.2)
==2380== by 0x4E81CF9: g_source_new (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.2)
==2380== by 0x4E85071: g_idle_source_new (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.2)
==2380== by 0x521B9C5: ??? (in /usr/lib/x86_64-linux-gnu/libgio-2.0.so.0.4800.2)
==2380== by 0x52238D1: g_dbus_connection_signal_unsubscribe (in /usr/lib/x86_64-linux-gnu/libgio-2.0.so.0.4800.2)
==2380== by 0x522D3F0: ??? (in /usr/lib/x86_64-linux-gnu/libgio-2.0.so.0.4800.2)
==2380== by 0x54E7D49: g_object_unref (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4800.2)
==2380== by 0x400B70: main (b.c:38)
==2380==
==2380== LEAK SUMMARY:
==2380== definitely lost: 0 bytes in 0 blocks
==2380== indirectly lost: 0 bytes in 0 blocks
==2380== possibly lost: 2,304 bytes in 26 blocks
==2380== still reachable: 7,324,158 bytes in 154,483 blocks
==2380== of which reachable via heuristic:
==2380== length64 : 768 bytes in 15 blocks
==2380== newarray : 1,728 bytes in 28 blocks
==2380== suppressed: 0 bytes in 0 blocks
==2380==
==2380== For counts of detected and suppressed errors, rerun with: -v
==2380== ERROR SUMMARY: 26 errors from 26 contexts (suppressed: 0 from 0)

it also support “ERROR SUMMARY: 26 errors from 26 contexts”
i don’t is it caused the wrong?

The program appears to be pushing idle sources but never runs the main loop. Try putting this somewhere before the usleep:

while (g_main_context_pending(NULL))
    g_main_context_iteration(NULL, TRUE);

You could also switch to using g_main_loop_run, and replace the while/usleep loop with g_timeout_add.

Very very thanks for your reply. I will test it today.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.