Always remove, invalidate and free watches before closing watched sockets
[platform/upstream/dbus.git] / bus / dir-watch-kqueue.c
index 4a01b74..62f7b3d 100644 (file)
@@ -81,6 +81,7 @@ _handle_kqueue_watch (DBusWatch *watch, unsigned int flags, void *data)
       if (watch != NULL)
        {
          _dbus_loop_remove_watch (loop, watch, _kqueue_watch_callback, NULL);
+          _dbus_watch_invalidate (watch);
           _dbus_watch_unref (watch);
          watch = NULL;
        }
@@ -124,10 +125,11 @@ _init_kqueue (BusContext *context)
                                    NULL, NULL))
           {
             _dbus_warn ("Unable to add reload watch to main loop");
-           close (kq);
-           kq = -1;
+           _dbus_watch_invalidate (watch);
            _dbus_watch_unref (watch);
            watch = NULL;
+           close (kq);
+           kq = -1;
             goto out;
          }
     }
@@ -169,7 +171,7 @@ bus_set_watched_dirs (BusContext *context, DBusList **directories)
    */
   for (i = 0; new_dirs[i]; i++)
     {
-      for (j = 0; i < num_fds; j++)
+      for (j = 0; j < num_fds; j++)
         {
           if (dirs[j] && strcmp (new_dirs[i], dirs[j]) == 0)
             {