<div dir="ltr">Hi!<br><br>I stumbled upon a memory leak with
attached silly test program (attached below), and while doing some
testing i found out that i need to call the 'sd_bus_process' after <br>'sd_bus_release_name' to avoid memory leak<br><br>The leak shows up with valgrind, and looks like<br>==18842== 3,402 (1,800 direct, 1,602 indirect) bytes in 1 blocks are definitely lost in loss record 11 of 12<br>==18842== at 0x4C31B25: calloc (in /usr/lib/valgrind/vgpreload_<wbr>memcheck-amd64-linux.so)<br>==18842== by 0x4E8AC2F: sd_bus_new (sd-bus.c:177)<br>==18842== by 0x4E8E5C1: sd_bus_open_user (sd-bus.c:1138)<br>==18842== by 0x108D47: Component::dbus_open() (in /XXX/sdtest)<br>==18842== by 0x108BCC: main (in /XXX/sdtest)<br> <br><br><div>On the appendix there is the 'FIXME' marker with line that makes the memory leak go away. <br></div><div><br></div><div>Is
this lack of documentation or a bug? I found the issue with stock
ubuntu 17.04 version - 234-2ubuntu12.1 -- and recompiled current git
head 82a18dcfb23fe5f259a2eb555fb90e<wbr>3a3475f12d and the issue seems to be in both versions.</div><div><br></div><div>I
was about to do man-page pull request, but started to hesitate, since
the code in the release looks like such that it would handle some
messaging.<br></div><div><br></div><div><br></div><div>Cheers,</div><div>Pauli<br></div><div><br></div><div><br><br><br></div>=== Appendix A: test program ===<br><br>#include <stdio.h><br>#include <stdlib.h><br>#include <systemd/sd-bus.h><br>#include <assert.h><br><br>class Component<br>{<br>public:<br>Component( const char* name ) : dbus( NULL )<br>{<br> this->name = name;<br>}<br> <br> <br>~Component()<br>{<br> if ( this->dbus )<br> {<br> printf("Destructor called!\n");<br> assert( sd_bus_release_name( dbus, this->name ) >= 0 );<br> <br> loop(); // FIXME: Commenting this makes it leak memory<br> sd_bus_flush(dbus); <br> sd_bus_unref(dbus);<br> }<br>}<br> <br> <br> bool loop()<br> {<br> for ( ;; )<br> {<br> int ret = sd_bus_process( dbus, NULL);<br> assert ( ret >= 0 );<br> <br> if ( ret > 0 )<br> continue;<br> <br> break;<br> }<br> return true;<br> }<br> <br> bool dbus_open()<br> {<br> int r;<br> r = sd_bus_open_user(&dbus);<br> if (r < 0) <br> {<br> printf( "Failed to connect to system bus: %s\n", strerror(-r) );<br> return false;<br> }<br> <br> <br> // Take a well-known service name so that clients can find us <br> r = sd_bus_request_name(dbus, this->name , 0);<br> if (r < 0) <br> {<br> printf("Failed to acquire service name: %s\n", strerror(-r));<br> return false;<br> }<br> <br> return true;<br> }<br> <br>private:<br> sd_bus* dbus;<br> const char* name;<br>};<br><br><br>int main()<br>{<br> Component com{ "com.example.foobar" };<br> printf("Hello there!\n");<br> com.dbus_open();<br> com.loop();<br> printf("Bye then!\n");<br>}</div>