[gtksourceview/wip/search] search: avoid memory leak of GTask and GCancellable



commit 1395a447b2d19db1a0d72d60f55195d654fbc09b
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Jul 6 16:33:55 2013 +0200

    search: avoid memory leak of GTask and GCancellable

 gtksourceview/gtksourcebuffer.c |   18 +++++++++++
 gtksourceview/gtksourcesearch.c |   62 +++++++++++++++++++--------------------
 2 files changed, 48 insertions(+), 32 deletions(-)
---
diff --git a/gtksourceview/gtksourcebuffer.c b/gtksourceview/gtksourcebuffer.c
index 7327100..5b02487 100644
--- a/gtksourceview/gtksourcebuffer.c
+++ b/gtksourceview/gtksourcebuffer.c
@@ -2810,6 +2810,8 @@ gtk_source_buffer_get_search_occurrence_position (GtkSourceBuffer   *buffer,
  * If @iter is %NULL, the search begins at the end of the buffer selection.
  *
  * Returns: whether a match was found.
+ *
+ * Since: 3.10
  */
 gboolean
 gtk_source_buffer_forward_search (GtkSourceBuffer   *buffer,
@@ -2825,6 +2827,22 @@ gtk_source_buffer_forward_search (GtkSourceBuffer   *buffer,
                                           match_end);
 }
 
+/**
+ * gtk_source_buffer_forward_search_async:
+ * @buffer: a #GtkSourceBuffer.
+ * @iter: start of search, or %NULL.
+ * @cancellable: a #GCancellable, or %NULL.
+ * @callback: a #GAsyncReadyCallback to call when the operation is finished.
+ * @user_data: the data to pass to the @callback function.
+ *
+ * Asynchronous forward search.
+ *
+ * If the operation is cancelled, the @callback will only be called if
+ * @cancellable was not %NULL. gtk_source_buffer_forward_search_async() takes
+ * ownership of @cancellable, so you can unref it after calling this function.
+ *
+ * Since: 3.10
+ */
 void
 gtk_source_buffer_forward_search_async (GtkSourceBuffer     *buffer,
                                        const GtkTextIter   *iter,
diff --git a/gtksourceview/gtksourcesearch.c b/gtksourceview/gtksourcesearch.c
index 6b3fe66..276ad74 100644
--- a/gtksourceview/gtksourcesearch.c
+++ b/gtksourceview/gtksourcesearch.c
@@ -351,6 +351,7 @@ clear_task (GtkSourceSearch *search)
                if (cancellable != NULL)
                {
                        g_cancellable_cancel (cancellable);
+                       g_task_return_error_if_cancelled (search->priv->task);
                }
 
                g_clear_object (&search->priv->task);
@@ -475,7 +476,6 @@ forward_backward_data_free (ForwardBackwardData *data)
 static gboolean
 smart_forward_search_async_step (GtkSourceSearch *search,
                                 GtkTextIter     *start_at,
-                                GTask           *task,
                                 gboolean        *wrapped_around)
 {
        GtkTextIter iter = *start_at;
@@ -499,11 +499,11 @@ smart_forward_search_async_step (GtkSourceSearch *search,
                task_data->is_forward = TRUE;
                task_data->wrapped_around = *wrapped_around;
 
-               g_task_return_pointer (task,
+               g_task_return_pointer (search->priv->task,
                                       task_data,
                                       (GDestroyNotify)forward_backward_data_free);
 
-               g_object_unref (task);
+               g_clear_object (&search->priv->task);
                return TRUE;
        }
 
@@ -542,11 +542,11 @@ smart_forward_search_async_step (GtkSourceSearch *search,
                        task_data->is_forward = TRUE;
                        task_data->wrapped_around = *wrapped_around;
 
-                       g_task_return_pointer (task,
+                       g_task_return_pointer (search->priv->task,
                                               task_data,
                                               (GDestroyNotify)forward_backward_data_free);
 
-                       g_object_unref (task);
+                       g_clear_object (&search->priv->task);
                        return TRUE;
                }
 
@@ -562,12 +562,15 @@ smart_forward_search_async_step (GtkSourceSearch *search,
                                                           start_at,
                                                           TRUE);
 
-       g_task_set_task_data (task,
+       g_task_set_task_data (search->priv->task,
                              task_data,
                              (GDestroyNotify)forward_backward_data_free);
 
-       clear_task (search);
-       search->priv->task = g_object_ref (task);
+       if (search->priv->task_region != NULL)
+       {
+               gtk_text_region_destroy (search->priv->task_region, TRUE);
+       }
+
        search->priv->task_region = region;
 
        install_idle_scan (search);
@@ -582,7 +585,6 @@ smart_forward_search_async_step (GtkSourceSearch *search,
 static void
 smart_forward_search_async (GtkSourceSearch   *search,
                            const GtkTextIter *start_at,
-                           GTask             *task,
                            gboolean           wrapped_around)
 {
        GtkTextIter iter = *start_at;
@@ -595,7 +597,7 @@ smart_forward_search_async (GtkSourceSearch   *search,
        /* A recursive function would have been more natural, but a loop is
         * better to avoid stack overflows.
         */
-       while (!smart_forward_search_async_step (search, &iter, task, &wrapped_around));
+       while (!smart_forward_search_async_step (search, &iter, &wrapped_around));
 }
 
 
@@ -603,7 +605,6 @@ smart_forward_search_async (GtkSourceSearch   *search,
 static gboolean
 smart_backward_search_async_step (GtkSourceSearch *search,
                                  GtkTextIter     *start_at,
-                                 GTask           *task,
                                  gboolean        *wrapped_around)
 {
        GtkTextIter iter = *start_at;
@@ -627,11 +628,11 @@ smart_backward_search_async_step (GtkSourceSearch *search,
                task_data->is_forward = FALSE;
                task_data->wrapped_around = *wrapped_around;
 
-               g_task_return_pointer (task,
+               g_task_return_pointer (search->priv->task,
                                       task_data,
                                       (GDestroyNotify)forward_backward_data_free);
 
-               g_object_unref (task);
+               g_clear_object (&search->priv->task);
                return TRUE;
        }
 
@@ -672,11 +673,11 @@ smart_backward_search_async_step (GtkSourceSearch *search,
                        task_data->is_forward = FALSE;
                        task_data->wrapped_around = *wrapped_around;
 
-                       g_task_return_pointer (task,
+                       g_task_return_pointer (search->priv->task,
                                               task_data,
                                               (GDestroyNotify)forward_backward_data_free);
 
-                       g_object_unref (task);
+                       g_clear_object (&search->priv->task);
                        return TRUE;
                }
 
@@ -692,12 +693,15 @@ smart_backward_search_async_step (GtkSourceSearch *search,
                                                           start_at,
                                                           TRUE);
 
-       g_task_set_task_data (task,
+       g_task_set_task_data (search->priv->task,
                              task_data,
                              (GDestroyNotify)forward_backward_data_free);
 
-       clear_task (search);
-       search->priv->task = g_object_ref (task);
+       if (search->priv->task_region != NULL)
+       {
+               gtk_text_region_destroy (search->priv->task_region, TRUE);
+       }
+
        search->priv->task_region = region;
 
        install_idle_scan (search);
@@ -712,7 +716,6 @@ smart_backward_search_async_step (GtkSourceSearch *search,
 static void
 smart_backward_search_async (GtkSourceSearch   *search,
                             const GtkTextIter *start_at,
-                            GTask             *task,
                             gboolean           wrapped_around)
 {
        GtkTextIter iter = *start_at;
@@ -725,7 +728,7 @@ smart_backward_search_async (GtkSourceSearch   *search,
        /* A recursive function would have been more natural, but a loop is
         * better to avoid stack overflows.
         */
-       while (!smart_backward_search_async_step (search, &iter, task, &wrapped_around));
+       while (!smart_backward_search_async_step (search, &iter, &wrapped_around));
 }
 
 /* Adjust the subregion so we are sure that all matches that are visible or
@@ -1167,8 +1170,7 @@ scan_region_backward (GtkSourceSearch *search,
 static void
 scan_task_region (GtkSourceSearch *search)
 {
-       GTask *task = search->priv->task;
-       ForwardBackwardData *task_data = g_task_get_task_data (task);
+       ForwardBackwardData *task_data = g_task_get_task_data (search->priv->task);
        GtkTextIter start_at;
 
        if (task_data->is_forward)
@@ -1186,8 +1188,6 @@ scan_task_region (GtkSourceSearch *search)
                search->priv->task_region = NULL;
        }
 
-       g_clear_object (&search->priv->task);
-
        gtk_text_buffer_get_iter_at_mark (search->priv->buffer,
                                          &start_at,
                                          task_data->start_at);
@@ -1196,14 +1196,12 @@ scan_task_region (GtkSourceSearch *search)
        {
                smart_forward_search_async (search,
                                            &start_at,
-                                           task,
                                            task_data->wrapped_around);
        }
        else
        {
                smart_backward_search_async (search,
                                             &start_at,
-                                            task,
                                             task_data->wrapped_around);
        }
 }
@@ -1938,7 +1936,6 @@ _gtk_source_search_forward_async (GtkSourceSearch     *search,
                                  GAsyncReadyCallback  callback,
                                  gpointer             user_data)
 {
-       GTask *task;
        GtkTextIter start_at;
 
        g_return_if_fail (GTK_SOURCE_IS_SEARCH (search));
@@ -1948,7 +1945,8 @@ _gtk_source_search_forward_async (GtkSourceSearch     *search,
                return;
        }
 
-       task = g_task_new (search->priv->buffer, cancellable, callback, user_data);
+       clear_task (search);
+       search->priv->task = g_task_new (search->priv->buffer, cancellable, callback, user_data);
 
        if (iter != NULL)
        {
@@ -1961,7 +1959,7 @@ _gtk_source_search_forward_async (GtkSourceSearch     *search,
                                                      &start_at);
        }
 
-       smart_forward_search_async (search, &start_at, task, FALSE);
+       smart_forward_search_async (search, &start_at, FALSE);
 }
 
 gboolean
@@ -2055,7 +2053,6 @@ _gtk_source_search_backward_async (GtkSourceSearch     *search,
                                   GAsyncReadyCallback  callback,
                                   gpointer             user_data)
 {
-       GTask *task;
        GtkTextIter start_at;
 
        g_return_if_fail (GTK_SOURCE_IS_SEARCH (search));
@@ -2065,7 +2062,8 @@ _gtk_source_search_backward_async (GtkSourceSearch     *search,
                return;
        }
 
-       task = g_task_new (search->priv->buffer, cancellable, callback, user_data);
+       clear_task (search);
+       search->priv->task = g_task_new (search->priv->buffer, cancellable, callback, user_data);
 
        if (iter != NULL)
        {
@@ -2078,7 +2076,7 @@ _gtk_source_search_backward_async (GtkSourceSearch     *search,
                                                      NULL);
        }
 
-       smart_backward_search_async (search, &start_at, task, FALSE);
+       smart_backward_search_async (search, &start_at, FALSE);
 }
 
 gboolean



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