I'm working on writing a mainloop, mostly just trying to figure out what's needed.  It looks like for an asynchronous program using dbus, one should really use the DBusWatch structures, so that's what I'm trying to figure out.  My issue right now is what memory is mine to manage, and what is owned and managed by the dbus library.  In my trivial example program (listed below), I just connect to the bus and call dbus_connection_set_watch_functions with my stub functions to see what happens.  My program prints out every time a watch is added, removed, toggled, and deleted.  Through the run of my program, two watches are added, none are removed, toggled, or deleted.  Both added watches have the same associated file descriptor, but different addresses.  The first call is given a disabled watch, and the second is given an enabled one.  
<br><br>Since the watches are supposed to be used for select(), I assume that it doesn&#39;t make sense to keep track of repeated watch additions that have the same file descriptor.&nbsp; Should I just free redundant watches, or is dbus doing that behind my back?&nbsp; Or, if I were to write a somewhat less trivial program, would the out-of-date watch be eventually given to the remove and free functions that were registered with dbus_connection_set_watch_functions?
<br><br>Code listing:<br><br>#include&lt;dbus/dbus.h&gt;<br>#include&lt;stdio.h&gt;<br><br>dbus_bool_t addWatch(DBusWatch *w, void *d)<br>{<br>&nbsp; int fd = dbus_watch_get_fd(w);<br>&nbsp; int e&nbsp; = dbus_watch_get_enabled(w);<br><br>
&nbsp; printf(&quot;Adding watch with address %d\n&quot;, (int)w);<br>&nbsp; printf(&quot; - fd is %d\n&quot;, fd);<br>&nbsp; printf(&quot; - state is %s\n&quot;, e?&quot;enabled&quot;:&quot;disabled&quot;);<br>}<br><br>void delWatch(DBusWatch *w, void *d)
<br>{<br>&nbsp; printf(&quot;Deleting watch at %d\n&quot;, (int)w);<br>}<br><br>void toggleWatch(DBusWatch *w, void *d)<br>{<br>&nbsp; printf(&quot;Toggling watch at %d\n&quot;, (int)w);<br>}<br><br>void mfree(void *data)<br>{<br>&nbsp; printf(&quot;Freeing data at %d\n&quot;, (int)data);
<br>&nbsp; dbus_free(data);<br>}<br><br>int main() {<br>&nbsp; DBusError err;<br>&nbsp; DBusConnection* conn;<br><br>&nbsp; dbus_error_init(&amp;err);<br>&nbsp; conn = dbus_bus_get(DBUS_BUS_SESSION, &amp;err);<br>&nbsp; if (dbus_error_is_set(&amp;err)) {
<br>&nbsp;&nbsp;&nbsp; fprintf(stderr, &quot;Connection Error (%s)\n&quot;, err.message);<br>&nbsp;&nbsp;&nbsp; dbus_error_free(&amp;err);<br>&nbsp; }<br>&nbsp; if (NULL == conn) {<br>&nbsp;&nbsp;&nbsp; exit(1);<br>&nbsp; }<br><br>&nbsp; if(FALSE == dbus_connection_set_watch_functions(conn, addWatch, delWatch, toggleWatch, 0, dbus_free)) {
<br>&nbsp;&nbsp;&nbsp; fprintf(stderr, &quot;Couldn&#39;t set up watch functions\n&quot;);<br>&nbsp;&nbsp;&nbsp; exit(1);<br>&nbsp; }<br><br>&nbsp; exit(0);<br>}<br><br><br>