dbus cleanup question

Macartney, Geoff gmacartney at tycoint.com
Thu Feb 21 05:17:38 PST 2008

Hi Havoc et al.

I have a followup question to my question about cleanup on shutdown. 

I tried adding dbus_shutdown (I was already calling dbus_unref on my
connection) upon exit, and I get the error message:

process 12278: dbus_shutdown() called but connections were still live.
This probably means the application did not drop all its references to
bus connections.

plus valgrind reports >6k bytes still reachable (i.e. they haven't been

Now I don't get the message if the shutdown is because of receiving an
org.freedesktop.DBus.Local.Disconnected (upon shutting down the bus
demon, for test purposes), so it seems like the Disconnected causes
libdbus to clean up its connections and internal resources, as you'd
expect. (Note that I have dbus_connection_set_exit_on_disconnect FALSE).
In this situation, if I just exit without dbus_shutdown, valgrind
reports >2k bytes still reachable, so it seems like the receipt of the
Disconnected causes cleanup of most stuff, but it still requires you to
do the dbus_shutdown to release the last of its memory resources.

BUT I want to be able to send my *own* "Stop" message to my application
and have it exit its event loop gracefully (without the error message
about connections still live). Now I could just avoid calling
dbus_shutdown, but what I'm writing is a library with its own event
loop, so I can't be sure that the application using it will exit when
the Stop gets sent. In theory it may choose to stop for now, but
re-connect to Dbus later on (going back into the event loop).

So at present I'm faced with a situation where either I don't call
shutdown, in which case of course my loop will exit with libdbus not
closed and cleaned, or I do call shutdown, but get this complaint about
live connections as shown above.

I daresay I'm missing something obvious, but I can't see what.

Any help will be much appreciated. I'm not just thinking about possible
memory leaks when I shutdown, but would like to avoid other possible
resource leaks - I don't know, for example, if I might leave connections
lying around if I don't do a dbus_shutdown?

Best regards
Geoff Macartney


Tyco Safety Products/CEM Systems Ltd.
Registered in Northern Ireland: NI 25728.  Registered Office: Unit 4 Ravenhill Business Park, Ravenhill Road, Belfast, BT6 8AW..
Please note that any views or opinions presented in this email are solely those of the author and do not necessarily represent those of the company. This email and any attachments are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the sender immediately and delete any copies in your possession.
-----Original Message-----

From: havoc.pennington at gmail.com [mailto:havoc.pennington at gmail.com] On
Behalf Of Havoc Pennington
Sent: 19 February 2008 12:50
To: Macartney, Geoff
Cc: dbus at lists.freedesktop.org
Subject: Re: dbus cleanup question


On Feb 19, 2008 7:06 AM, Macartney, Geoff <gmacartney at tycoint.com>
> I'm using the low level API to dbus (working on an embedded platform).
> cleanup do I need to do when shutting down, to prevent any resource
> My programs open a connection to the system bus, and I'm assuming I
need to
> call dbus_connection_unref on the connection when I'm done with it.
But what
> about the following?

unref anything you have a ref to and dbus_shutdown() generally. (If
your program is not exiting. If your app is exiting, just exit,
dbus_shutdown() is pointless.)

> If I've added matches to the connection using dbus_bus_add_match, do I
> to explicitly remove them with dbus_bus_remove_match before I unref
> connection?

No, because this resource (the match) is in the bus daemon, and the
daemon will free it when you disconnect.

> If I've added handlers for object paths using
> dbus_connection_register_fallback or
> do I need to remove them using dbus_connection_unregister_object_path
> I unref the connection?

If the connection is finalized (refcount reaches 0) your object path
is automatically unregistered. However, typically it's better to
unregister an object path at the point where you don't want it invoked
anymore; after all, someone else may be holding a refcount, and you
don't want the object path registration to accidentally stay alive,
especially if you freed the object it refers to.


More information about the dbus mailing list