[systemd-devel] sd-bus memory check

William Kennington william at wkennington.com
Mon Apr 13 01:20:13 UTC 2020


You are probably looking for sd_bus_flush_close_unref() for this usecase.

On Sun, Apr 12, 2020 at 11:00 AM David J <emacsd at icloud.com> wrote:

> Hello!
>
> This is in regards to sd-bus function "sd_bus_open_system", where Valgrind
> reports possible memory leak. See the following code:
>
> mem_test.c:
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <systemd/sd-bus.h>
>
> int main(int argc, char *argv[]) {
>   sd_bus *bus = NULL;
>
>   int r = sd_bus_open_system(&bus);
>   if (r < 0) {
>     fprintf(stderr, "sd_bus_open_system: %s\n", strerror(-r));
>   }
>   bus = sd_bus_unref(bus);
> }
>
> compile command: gcc mem_test.c -o mem_test `pkg-config --cflags --libs
> libsystemd`
>
> Valgrind output:
>
> ==4452== Memcheck, a memory error detector
> ==4452== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
> ==4452== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
> ==4452== Command: ./mem_test
> ==4452==
> ==4452==
> ==4452== HEAP SUMMARY:
> ==4452==     in use at exit: 7,736 bytes in 12 blocks
> ==4452==   total heap usage: 17 allocs, 5 frees, 8,045 bytes allocated
> ==4452==
> ==4452== LEAK SUMMARY:
> ==4452==    definitely lost: 0 bytes in 0 blocks
> ==4452==    indirectly lost: 0 bytes in 0 blocks
> ==4452==      possibly lost: 3,640 bytes in 11 blocks
> ==4452==    still reachable: 4,096 bytes in 1 blocks
> ==4452==         suppressed: 0 bytes in 0 blocks
>
> The question is am I doing anything wrong here? Why Valgrind thinks there
> "might" be memory leak? The interesting part is if I use
> "sd_bus_open_system", Valgrind is all happy and no warnings at all!
>
> Thank you,
> David J.
>
>
>
> _______________________________________________
> systemd-devel mailing list
> systemd-devel at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/systemd-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/systemd-devel/attachments/20200412/cb1ff29d/attachment.htm>


More information about the systemd-devel mailing list