[systemd-devel] Handling memory in sd-bus

Iñigo Martínez inigomartinez at gmail.com
Fri Feb 26 09:20:01 UTC 2016


Hello,

Recently I have noticed some memory leaks in a program I wrote that
uses the sd-bus library for exposing a few properties and methods
through D-Bus.

After analyzing the memory use with valgrind, I have noticed some
weird messages regarding my use of sd-bus API, and I'm not sure if I'm
using it properly.

More or less this is what I'm doing (excluding error checking and other things):

    1. open system bus:

        sd_bus_open_system (&bus);

    2. add object vtable

        sd_bus_add (bus, &slot, path, iface, vtable, userdata);

    3. request a name:

        sd_bus_request_name (bus, iface);

    4 The main loop where I process and wait for messages:

        sd_bus_process (bus, NULL);
        sd_bus_wait (bus, (uint64_t) -1))

    5. When the loop ends, free everything in reverse order. First
release the name (3. step reverse):

        sd_bus_release_name (bus, iface);

    6. Release the slot (2. step reverse):

        sd_bus_slot_unref (slot);

    7. Finally, free the bus (1. step reverse):

    sd_bus_unref (bus).

Having all this steps in mind, these are the messages valgrind shows to me:

==6822== 2 bytes in 1 blocks are indirectly lost in loss record 1 of 41
==6822==    at 0x4C29C0F: malloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6822==    by 0x5FAD0A9: strdup (in /lib/x86_64-linux-gnu/libc-2.21.so)
==6822==    by 0x409F385: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x4088593: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x4085917: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x40921BE: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x4091E16: sd_bus_call (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x4079A4B: sd_bus_call_method (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x409BAD5: sd_bus_release_name (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x403D2E: pbus_free (pbus.c:79)
==6822==    by 0x402CA6: main (main.c:237)
==6822==
==6822== 3 bytes in 1 blocks are indirectly lost in loss record 2 of 41
==6822==    at 0x4C29C0F: malloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6822==    by 0x405E875: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x408989B: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x4058048: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x40AA809: sd_bus_start (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x40AAD32: sd_bus_open_system (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x403B23: pbus_init (pbus.c:38)
==6822==    by 0x402834: main (main.c:144)
==6822==
==6822== 7 bytes in 1 blocks are indirectly lost in loss record 3 of 41
==6822==    at 0x4C29C0F: malloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6822==    by 0x5FAD0A9: strdup (in /lib/x86_64-linux-gnu/libc-2.21.so)
==6822==    by 0x40592E9: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x4091282: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x4091B29: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x4091D1E: sd_bus_call (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x4079A4B: sd_bus_call_method (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x409BDC5: sd_bus_request_name (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x403C0A: pbus_init (pbus.c:56)
==6822==    by 0x402834: main (main.c:144)
==6822==
==6822== 24 bytes in 1 blocks are indirectly lost in loss record 8 of 41
==6822==    at 0x4C2BBD5: calloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6822==    by 0x4065E31: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x40917E4: sd_bus_call_async (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x40AA966: sd_bus_start (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x40AAD32: sd_bus_open_system (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x403B23: pbus_init (pbus.c:38)
==6822==    by 0x402834: main (main.c:144)
==6822==
==6822== 64 bytes in 1 blocks are indirectly lost in loss record 12 of 41
==6822==    at 0x4C29C0F: malloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6822==    by 0x4C2BE6F: realloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6822==    by 0x405A13D: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x405758C: sd_bus_new (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x40AACCB: sd_bus_open_system (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x403B23: pbus_init (pbus.c:38)
==6822==    by 0x402834: main (main.c:144)
==6822==
==6822== 64 bytes in 1 blocks are indirectly lost in loss record 13 of 41
==6822==    at 0x4C29C0F: malloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6822==    by 0x4C2BE6F: realloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6822==    by 0x405A13D: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x409215D: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x40884C6: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x4085917: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x40921BE: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x4090BC0: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x4091B29: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x4091D1E: sd_bus_call (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x4079A4B: sd_bus_call_method (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x409BDC5: sd_bus_request_name (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==
==6822== 81 bytes in 1 blocks are indirectly lost in loss record 15 of 41
==6822==    at 0x4C29C0F: malloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6822==    by 0x5FAD0A9: strdup (in /lib/x86_64-linux-gnu/libc-2.21.so)
==6822==    by 0x40573EF: sd_bus_set_address (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x40AAD8E: sd_bus_open_system (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x403B23: pbus_init (pbus.c:38)
==6822==    by 0x402834: main (main.c:144)
==6822==
==6822== 128 bytes in 1 blocks are still reachable in loss record 16 of 41
==6822==    at 0x4C29C0F: malloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6822==    by 0x4C2BE6F: realloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6822==    by 0x407F6D5: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x405E266: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x40815E6: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x408167D: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x408167D: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x407E4AD: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x407EB3F: sd_bus_add_object_vtable (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x403B98: pbus_init (pbus.c:44)
==6822==    by 0x402834: main (main.c:144)
==6822==
==6822== 128 bytes in 1 blocks are still reachable in loss record 17 of 41
==6822==    at 0x4C29C0F: malloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6822==    by 0x4C2BE6F: realloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6822==    by 0x407F6D5: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x405E266: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x407E698: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x407EB3F: sd_bus_add_object_vtable (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x403B98: pbus_init (pbus.c:44)
==6822==    by 0x402834: main (main.c:144)
==6822==
==6822== 193 bytes in 1 blocks are indirectly lost in loss record 18 of 41
==6822==    at 0x4C2BDDF: realloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6822==    by 0x40857ED: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x40921BE: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x4091E16: sd_bus_call (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x4079A4B: sd_bus_call_method (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x409BAD5: sd_bus_release_name (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x403D2E: pbus_free (pbus.c:79)
==6822==    by 0x402CA6: main (main.c:237)
==6822==
==6822== 256 bytes in 1 blocks are indirectly lost in loss record 19 of 41
==6822==    at 0x4C29C0F: malloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6822==    by 0x4C2BE6F: realloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6822==    by 0x4065D91: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x40918D1: sd_bus_call_async (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x40AA966: sd_bus_start (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x40AAD32: sd_bus_open_system (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x403B23: pbus_init (pbus.c:38)
==6822==    by 0x402834: main (main.c:144)
==6822==
==6822== 928 bytes in 1 blocks are indirectly lost in loss record 21 of 41
==6822==    at 0x4C2BBD5: calloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6822==    by 0x4080F95: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x408850E: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x4085917: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x40921BE: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x4091E16: sd_bus_call (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x4079A4B: sd_bus_call_method (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x409BAD5: sd_bus_release_name (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x403D2E: pbus_free (pbus.c:79)
==6822==    by 0x402CA6: main (main.c:237)
==6822==
==6822== 3,550 (1,928 direct, 1,622 indirect) bytes in 1 blocks are
definitely lost in loss record 26 of 41
==6822==    at 0x4C2BBD5: calloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6822==    by 0x405750E: sd_bus_new (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x40AACCB: sd_bus_open_system (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x403B23: pbus_init (pbus.c:38)
==6822==    by 0x402834: main (main.c:144)
==6822==
==6822== 4,096 bytes in 1 blocks are still reachable in loss record 27 of 41
==6822==    at 0x4C29C0F: malloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6822==    by 0x405D171: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x405D1E9: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x40917C7: sd_bus_call_async (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x40AA966: sd_bus_start (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x40AAD32: sd_bus_open_system (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x403B23: pbus_init (pbus.c:38)
==6822==    by 0x402834: main (main.c:144)
==6822==
==6822== 4,096 bytes in 1 blocks are still reachable in loss record 28 of 41
==6822==    at 0x4C29C0F: malloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6822==    by 0x405D171: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x405D1E9: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x407E47C: ??? (in /lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x407EB3F: sd_bus_add_object_vtable (in
/lib/x86_64-linux-gnu/libsystemd.so.0.14.0)
==6822==    by 0x403B98: pbus_init (pbus.c:44)
==6822==    by 0x402834: main (main.c:144)
==6822==

My questions are:

1. Are my steps correct when initializing and freeing the bus. For
example, I have seen some examples where they don't release the bus
name.
2. Are this valgrind messages known ? Are they something I should
ignore ? Or are they related to something I am doing wrong ?

My D-Bus and sd-bus knowledge is still limited, and probably there is
something I could be doing in a wrong way.

Thank you,

Best regards,


More information about the systemd-devel mailing list