chiark / gitweb /
bus: fix issue with reference counting
[elogind.git] / src / libsystemd / sd-bus.c
index 6bd1eaa..95d80db 100644 (file)
@@ -1259,12 +1259,25 @@ _public_ sd_bus *sd_bus_ref(sd_bus *bus) {
 }
 
 _public_ sd_bus *sd_bus_unref(sd_bus *bus) {
+        unsigned i;
 
         if (!bus)
                 return NULL;
 
-        if (REFCNT_DEC(bus->n_ref) <= 0)
-                bus_free(bus);
+        i = REFCNT_DEC(bus->n_ref);
+        if (i != bus->rqueue_size + bus->wqueue_size)
+                return NULL;
+
+        for (i = 0; i < bus->rqueue_size; i++)
+                if (bus->rqueue[i]->n_ref > 1)
+                        return NULL;
+
+        for (i = 0; i < bus->wqueue_size; i++)
+                if (bus->wqueue[i]->n_ref > 1)
+                        return NULL;
+
+        /* we are the only holders on the messages */
+        bus_free(bus);
 
         return NULL;
 }
@@ -1412,8 +1425,8 @@ static int dispatch_wqueue(sd_bus *bus) {
                          * it got full, then all bets are off
                          * anyway. */
 
-                        sd_bus_message_unref(bus->wqueue[0]);
                         bus->wqueue_size --;
+                        sd_bus_message_unref(bus->wqueue[0]);
                         memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size);
                         bus->windex = 0;