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't make sense to keep track of repeated watch additions that have the same file descriptor. Should I just free redundant watches, or is dbus doing that behind my back? 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<dbus/dbus.h><br>#include<stdio.h><br><br>dbus_bool_t addWatch(DBusWatch *w, void *d)<br>{<br> int fd = dbus_watch_get_fd(w);<br> int e = dbus_watch_get_enabled(w);<br><br>
printf("Adding watch with address %d\n", (int)w);<br> printf(" - fd is %d\n", fd);<br> printf(" - state is %s\n", e?"enabled":"disabled");<br>}<br><br>void delWatch(DBusWatch *w, void *d)
<br>{<br> printf("Deleting watch at %d\n", (int)w);<br>}<br><br>void toggleWatch(DBusWatch *w, void *d)<br>{<br> printf("Toggling watch at %d\n", (int)w);<br>}<br><br>void mfree(void *data)<br>{<br> printf("Freeing data at %d\n", (int)data);
<br> dbus_free(data);<br>}<br><br>int main() {<br> DBusError err;<br> DBusConnection* conn;<br><br> dbus_error_init(&err);<br> conn = dbus_bus_get(DBUS_BUS_SESSION, &err);<br> if (dbus_error_is_set(&err)) {
<br> fprintf(stderr, "Connection Error (%s)\n", err.message);<br> dbus_error_free(&err);<br> }<br> if (NULL == conn) {<br> exit(1);<br> }<br><br> if(FALSE == dbus_connection_set_watch_functions(conn, addWatch, delWatch, toggleWatch, 0, dbus_free)) {
<br> fprintf(stderr, "Couldn't set up watch functions\n");<br> exit(1);<br> }<br><br> exit(0);<br>}<br><br><br>