[devhelp/wip/swilmet/maintenance] book: fix bugs with the timeout



commit ed8d9288f2d42eb2d55b742f1edf384c110cfaad
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat May 23 18:08:02 2015 +0200

    book: fix bugs with the timeout
    
    - priv->monitor_event_timeout_id was not reset to 0.
    - when calling g_source_remove(), book was not unreffed.
    
    Fix those bugs by _not_ taking a ref to the book for the timeout.
    Instead, remove the timeout in dispose().

 src/dh-book.c |   25 ++++++++++++++++---------
 1 files changed, 16 insertions(+), 9 deletions(-)
---
diff --git a/src/dh-book.c b/src/dh-book.c
index 2f4019c..e5f85a3 100644
--- a/src/dh-book.c
+++ b/src/dh-book.c
@@ -96,6 +96,11 @@ dh_book_dispose (GObject *object)
 
         g_clear_object (&priv->monitor);
 
+        if (priv->monitor_event_timeout_id != 0) {
+                g_source_remove (priv->monitor_event_timeout_id);
+                priv->monitor_event_timeout_id = 0;
+        }
+
         G_OBJECT_CLASS (dh_book_parent_class)->dispose (object);
 }
 
@@ -272,14 +277,19 @@ dh_book_new (const gchar *book_path)
 }
 
 static gboolean
-book_monitor_event_timeout_cb  (gpointer data)
+book_monitor_event_timeout_cb (gpointer data)
 {
-        DhBook     *book = data;
+        DhBook *book = data;
         DhBookPrivate *priv = dh_book_get_instance_private (book);
+        DhBookMonitorEvent monitor_event = priv->monitor_event;
+
+        /* Reset event */
+        priv->monitor_event = BOOK_MONITOR_EVENT_NONE;
+        priv->monitor_event_timeout_id = 0;
 
         /* We'll get either is_deleted OR is_updated,
          * not possible to have both or none */
-        switch (priv->monitor_event)
+        switch (monitor_event)
         {
         case BOOK_MONITOR_EVENT_DELETED:
                 /* Emit the signal, but make sure we hold a reference
@@ -299,12 +309,9 @@ book_monitor_event_timeout_cb  (gpointer data)
                 break;
         }
 
-        /* Reset event */
-        priv->monitor_event = BOOK_MONITOR_EVENT_NONE;
+        /* book can be destroyed here */
 
-        /* Destroy the reference we got in the timeout */
-        g_object_unref (book);
-        return FALSE;
+        return G_SOURCE_REMOVE;
 }
 
 static void
@@ -344,7 +351,7 @@ book_monitor_event_cb (GFileMonitor      *file_monitor,
                 }
                 priv->monitor_event_timeout_id = g_timeout_add_seconds (EVENT_MERGE_TIMEOUT_SECS,
                                                                         book_monitor_event_timeout_cb,
-                                                                        g_object_ref (book));
+                                                                        book);
         }
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]