[gtksourceview/wip/fix-search] SearchContext: fix crash when buffer destroyed during search (wip)



commit a72e3e5a421a9af6c429fb2a66d32ebc7674e57b
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed Mar 4 20:24:41 2015 +0100

    SearchContext: fix crash when buffer destroyed during search (wip)
    
    Since the SearchContext has a weak ref to the buffer (and vice-versa),
    the buffer can be destroyed during a search.
    
    TODO Make the code more robust, remove idle cb when buffer destroyed.

 gtksourceview/gtksourcesearchcontext.c |    3 +++
 tests/test-search-context.c            |   25 +++++++++++++++++++++++++
 2 files changed, 28 insertions(+), 0 deletions(-)
---
diff --git a/gtksourceview/gtksourcesearchcontext.c b/gtksourceview/gtksourcesearchcontext.c
index 8195af8..974a6d0 100644
--- a/gtksourceview/gtksourcesearchcontext.c
+++ b/gtksourceview/gtksourcesearchcontext.c
@@ -1592,6 +1592,7 @@ scan_all_region (GtkSourceSearchContext *search,
                return;
        }
 
+       /* TODO check return value */
        gtk_text_region_nth_subregion (region_to_highlight,
                                       0,
                                       &start_search,
@@ -2084,6 +2085,7 @@ idle_scan_regex_search (GtkSourceSearchContext *search)
 static gboolean
 idle_scan_cb (GtkSourceSearchContext *search)
 {
+       g_message ("idle scan");
        return gtk_source_search_settings_get_regex_enabled (search->priv->settings) ?
               idle_scan_regex_search (search) :
               idle_scan_normal_search (search);
@@ -2094,6 +2096,7 @@ install_idle_scan (GtkSourceSearchContext *search)
 {
        if (search->priv->idle_scan_id == 0)
        {
+               g_message ("install idle scan");
                search->priv->idle_scan_id = g_idle_add ((GSourceFunc)idle_scan_cb, search);
        }
 }
diff --git a/tests/test-search-context.c b/tests/test-search-context.c
index 8e3782b..eba32c9 100644
--- a/tests/test-search-context.c
+++ b/tests/test-search-context.c
@@ -1059,6 +1059,30 @@ test_regex_at_word_boundaries (void)
        g_object_unref (context);
 }
 
+static void
+test_destroy_buffer_during_search (void)
+{
+       GtkSourceBuffer *source_buffer = gtk_source_buffer_new (NULL);
+       GtkTextBuffer *text_buffer = GTK_TEXT_BUFFER (source_buffer);
+       GtkSourceSearchSettings *settings = gtk_source_search_settings_new ();
+       GtkSourceSearchContext *context = gtk_source_search_context_new (source_buffer, settings);
+
+       gtk_text_buffer_set_text (text_buffer, "y", -1);
+       gtk_source_search_settings_set_search_text (settings, "y");
+
+       /* Destroy buffer during search. */
+       g_object_unref (source_buffer);
+       g_message ("buffer destroyed");
+       flush_queue ();
+
+       /* Test also a new search when buffer already destroyed. */
+       gtk_source_search_settings_set_search_text (settings, "x");
+       flush_queue ();
+
+       g_object_unref (settings);
+       g_object_unref (context);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -1085,6 +1109,7 @@ main (int argc, char **argv)
        g_test_add_func ("/Search/replace", test_replace_all);
        g_test_add_func ("/Search/regex", test_regex);
        g_test_add_func ("/Search/regex-at-word-boundaries", test_regex_at_word_boundaries);
+       g_test_add_func ("/Search/destroy-buffer-during-search", test_destroy_buffer_during_search);
 
        return g_test_run ();
 }


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