Fix memory leak in notmuch_thread_results_t
authorCarl Worth <cworth@cworth.org>
Mon, 26 Oct 2009 21:02:58 +0000 (14:02 -0700)
committerCarl Worth <cworth@cworth.org>
Mon, 26 Oct 2009 21:02:58 +0000 (14:02 -0700)
If we were using a talloc-based resizing array then this wouldn't
have happened. Of course, thanks to valgrind for catching this.

query.cc

index 5fee854c8a6b8a22ff9da4bd04cbfd400a3c9f9c..4df2f52ff65ce8b39294828eb47c2dd0159320af 100644 (file)
--- a/query.cc
+++ b/query.cc
@@ -158,6 +158,18 @@ notmuch_query_search_messages (notmuch_query_t *query)
     return results;
 }
 
     return results;
 }
 
+/* Glib objects force use to use a talloc destructor as well, (but not
+ * nearly as ugly as the for message_results due to C++ objects). At
+ * this point, I'd really like to have some talloc-friendly
+ * equivalents for the few pieces of glib that I'm using. */
+static int
+_notmuch_thread_results_destructor (notmuch_thread_results_t *results)
+{
+    g_ptr_array_free (results->thread_ids, TRUE);
+
+    return 0;
+}
+
 notmuch_thread_results_t *
 notmuch_query_search_threads (notmuch_query_t *query)
 {
 notmuch_thread_results_t *
 notmuch_query_search_threads (notmuch_query_t *query)
 {
@@ -175,6 +187,8 @@ notmuch_query_search_threads (notmuch_query_t *query)
     thread_results->thread_ids = g_ptr_array_new ();
     thread_results->index = 0;
 
     thread_results->thread_ids = g_ptr_array_new ();
     thread_results->index = 0;
 
+    talloc_set_destructor (thread_results, _notmuch_thread_results_destructor);
+
     seen = g_hash_table_new_full (g_str_hash, g_str_equal,
                                  free, NULL);
 
     seen = g_hash_table_new_full (g_str_hash, g_str_equal,
                                  free, NULL);
 
@@ -285,6 +299,5 @@ notmuch_thread_results_advance (notmuch_thread_results_t *results)
 void
 notmuch_thread_results_destroy (notmuch_thread_results_t *results)
 {
 void
 notmuch_thread_results_destroy (notmuch_thread_results_t *results)
 {
-    g_ptr_array_free (results->thread_ids, TRUE);
     talloc_free (results);
 }
     talloc_free (results);
 }