[tracker/fts-property-names-cleanup: 3/3] libtracker-fts: Fixed memory leak for property_names variable



commit 0102cc7d0547aec0bcfa5d7ff5982707a1eed9f4
Author: Martyn Russell <martyn lanedo com>
Date:   Thu Aug 14 12:15:52 2014 +0100

    libtracker-fts: Fixed memory leak for property_names variable

 src/libtracker-common/tracker-type-utils.c        |   18 ++++-
 src/libtracker-common/tracker-type-utils.h        |    1 +
 src/libtracker-data/tracker-data-manager.c        |    9 ++-
 src/libtracker-data/tracker-db-interface-sqlite.c |   11 ++-
 src/libtracker-fts/tracker-fts.c                  |   83 +++++++++++++++------
 src/libtracker-fts/tracker-fts.h                  |   11 ++-
 6 files changed, 99 insertions(+), 34 deletions(-)
---
diff --git a/src/libtracker-common/tracker-type-utils.c b/src/libtracker-common/tracker-type-utils.c
index ef4f5a2..7b22166 100644
--- a/src/libtracker-common/tracker-type-utils.c
+++ b/src/libtracker-common/tracker-type-utils.c
@@ -210,14 +210,14 @@ tracker_string_to_string_list (const gchar *str)
        return result;
 }
 
-gchar **
-tracker_gslist_to_string_list (GSList *list)
+static gchar **
+list_to_string_list (GSList *list, gint length)
 {
        GSList  *l;
        gchar  **strv;
        gint     i;
 
-       strv = g_new0 (gchar*, g_slist_length (list) + 1);
+       strv = g_new0 (gchar*, length + 1);
 
        for (l = list, i = 0; l; l = l->next) {
                if (!l->data) {
@@ -232,6 +232,18 @@ tracker_gslist_to_string_list (GSList *list)
        return strv;
 }
 
+gchar **
+tracker_gslist_to_string_list (GSList *list)
+{
+       return list_to_string_list (list, g_slist_length (list));
+}
+
+gchar **
+tracker_glist_to_string_list (GList *list)
+{
+       return list_to_string_list ((GSList*) list, g_list_length (list));
+}
+
 gboolean
 tracker_gslist_with_string_data_equal (GSList *list1,
                                        GSList *list2)
diff --git a/src/libtracker-common/tracker-type-utils.h b/src/libtracker-common/tracker-type-utils.h
index cc34d61..6197ab7 100644
--- a/src/libtracker-common/tracker-type-utils.h
+++ b/src/libtracker-common/tracker-type-utils.h
@@ -47,6 +47,7 @@ gchar *  tracker_string_list_to_string         (gchar       **strv,
                                                 gchar         sep);
 gchar ** tracker_string_to_string_list         (const gchar  *str);
 gchar ** tracker_gslist_to_string_list         (GSList       *list);
+gchar ** tracker_glist_to_string_list          (GList        *list);
 gboolean tracker_gslist_with_string_data_equal (GSList       *list1,
                                                 GSList       *list2);
 GSList * tracker_gslist_copy_with_string_data  (GSList       *list);
diff --git a/src/libtracker-data/tracker-data-manager.c b/src/libtracker-data/tracker-data-manager.c
index abff7b4..03cb80b 100644
--- a/src/libtracker-data/tracker-data-manager.c
+++ b/src/libtracker-data/tracker-data-manager.c
@@ -3646,7 +3646,7 @@ tracker_data_manager_init (TrackerDBManagerFlags   flags,
 
 #ifdef HAVE_TRACKER_FTS
        if (!tracker_fts_init ()) {
-               g_warning ("FTS module loading failed");
+               g_warning ("FTS module initialization failed");
        }
 #endif
 
@@ -4614,6 +4614,13 @@ tracker_data_manager_shutdown (void)
        if (!reloading) {
                tracker_locale_shutdown ();
        }
+
+#ifdef HAVE_TRACKER_FTS
+       if (!tracker_fts_shutdown ()) {
+               g_warning ("FTS module shutdown failed");
+       }
+#endif
+
        tracker_data_update_shutdown ();
 
        initialized = FALSE;
diff --git a/src/libtracker-data/tracker-db-interface-sqlite.c 
b/src/libtracker-data/tracker-db-interface-sqlite.c
index 2610120..b35dd84 100644
--- a/src/libtracker-data/tracker-db-interface-sqlite.c
+++ b/src/libtracker-data/tracker-db-interface-sqlite.c
@@ -31,7 +31,7 @@
 
 #include <libtracker-sparql/tracker-sparql.h>
 
-#if HAVE_TRACKER_FTS
+#ifdef HAVE_TRACKER_FTS
 #include <libtracker-fts/tracker-fts.h>
 #include <libtracker-fts/tracker-parser.h>
 #endif
@@ -1145,7 +1145,7 @@ tracker_db_interface_sqlite_fts_init (TrackerDBInterface  *db_interface,
                                       GHashTable          *multivalued,
                                       gboolean             create)
 {
-#if HAVE_TRACKER_FTS
+#ifdef HAVE_TRACKER_FTS
        GStrv fts_columns;
 
        tracker_fts_init_db (db_interface->db, properties);
@@ -1185,7 +1185,8 @@ tracker_db_interface_sqlite_fts_init (TrackerDBInterface  *db_interface,
 #endif
 }
 
-#if HAVE_TRACKER_FTS
+#ifdef HAVE_TRACKER_FTS
+
 void
 tracker_db_interface_sqlite_fts_alter_table (TrackerDBInterface  *db_interface,
                                             GHashTable          *properties,
@@ -1333,6 +1334,10 @@ tracker_db_interface_sqlite_finalize (GObject *object)
 
        db_interface = TRACKER_DB_INTERFACE (object);
 
+#ifdef HAVE_TRACKER_FTS
+       tracker_fts_shutdown_db (db_interface->db);
+#endif
+
        close_database (db_interface);
        g_free (db_interface->fts_insert_str);
 
diff --git a/src/libtracker-fts/tracker-fts.c b/src/libtracker-fts/tracker-fts.c
index 77351e5..e8fb4c1 100644
--- a/src/libtracker-fts/tracker-fts.c
+++ b/src/libtracker-fts/tracker-fts.c
@@ -20,23 +20,36 @@
  */
 
 #include "config.h"
-#include <sqlite3.h>
+
+#include <libtracker-common/tracker-common.h>
+
 #include "tracker-fts-tokenizer.h"
 #include "tracker-fts.h"
 
 #ifndef HAVE_BUILTIN_FTS
-#  include "fts3.h"
+
+#include "fts3.h"
+static gsize      module_initialized = 0;
+
 #endif
 
-static gchar **property_names;
+static gchar    **property_names = NULL;
+static gboolean   initialized = FALSE;
+
 
 gboolean
-tracker_fts_init (void) {
+tracker_fts_init (void)
+{
+       if (initialized) {
+               return TRUE;
+       }
+
 #ifdef HAVE_BUILTIN_FTS
+       initialized = TRUE;
+
        /* SQLite has all needed FTS4 features compiled in */
        return TRUE;
 #else
-       static gsize module_initialized = 0;
        int rc = SQLITE_OK;
 
        if (g_once_init_enter (&module_initialized)) {
@@ -44,10 +57,25 @@ tracker_fts_init (void) {
                g_once_init_leave (&module_initialized, (rc == SQLITE_OK));
        }
 
-       return (module_initialized != 0);
+       initialized = module_initialized != 0;
+
+       return initialized;
 #endif
 }
 
+gboolean
+tracker_fts_shutdown (void)
+{
+       if (!initialized) {
+               return TRUE;
+       }
+
+       /* Nothing to do, there is no fts4_extension_shutdown() */
+       initialized = FALSE;
+
+       return TRUE;
+}
+
 static void
 function_rank (sqlite3_context *context,
                int              argc,
@@ -201,40 +229,45 @@ fts_register_functions (sqlite3 *db)
                                 NULL, NULL);
 }
 
-static void
-fts_init_property_names (GHashTable *tables)
+gboolean
+tracker_fts_init_db (sqlite3    *db,
+                     GHashTable *tables)
 {
        GHashTableIter iter;
-       GList *c;
        GList *columns;
        GList *table_columns;
-       gchar **ptr;
 
+       g_return_val_if_fail (initialized == TRUE, FALSE);
+
+       if (!tracker_tokenizer_initialize (db)) {
+               return FALSE;
+       }
+
+       /* Set up GStrv 'property_names' */
        columns = NULL;
        g_hash_table_iter_init (&iter, tables);
        while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &table_columns)) {
                columns = g_list_concat (columns, g_list_copy (table_columns));
        }
 
-       ptr = property_names = g_new0 (gchar *, g_list_length (columns));
-       for (c = columns; c!= NULL ; c = c->next) {
-               *ptr = g_strdup (c->data);
-               ptr ++;
-       }
-
+       property_names = tracker_glist_to_string_list (columns);
        g_list_free (columns);
+
+       /* Register functions with the database, including one to get property names */
+       fts_register_functions (db);
+
+       return TRUE;
 }
 
 gboolean
-tracker_fts_init_db (sqlite3 *db,
-                     GHashTable *tables)
+tracker_fts_shutdown_db (sqlite3 *db)
 {
-       if (!tracker_tokenizer_initialize (db)) {
-               return FALSE;
-       }
+       g_return_val_if_fail (initialized == TRUE, FALSE);
 
-       fts_init_property_names (tables);
-       fts_register_functions (db);
+       if (property_names != NULL) {
+               g_strfreev (property_names);
+               property_names = NULL;
+       }
 
        return TRUE;
 }
@@ -251,6 +284,8 @@ tracker_fts_create_table (sqlite3    *db,
        GList *columns;
        gint rc;
 
+       g_return_val_if_fail (initialized == TRUE, FALSE);
+
        /* Create view on tables/columns marked as FTS-indexed */
        g_hash_table_iter_init (&iter, tables);
        str = g_string_new ("CREATE VIEW fts_view AS SELECT Resource.ID as rowid ");
@@ -313,6 +348,8 @@ tracker_fts_alter_table (sqlite3    *db,
        gchar *query, *tmp_name;
        int rc;
 
+       g_return_val_if_fail (initialized == TRUE, FALSE);
+
        tmp_name = g_strdup_printf ("%s_TMP", table_name);
 
        query = g_strdup_printf ("DROP VIEW fts_view");
diff --git a/src/libtracker-fts/tracker-fts.h b/src/libtracker-fts/tracker-fts.h
index 7fdc0dc..df76a37 100644
--- a/src/libtracker-fts/tracker-fts.h
+++ b/src/libtracker-fts/tracker-fts.h
@@ -19,17 +19,20 @@
  * 02110-1301  USA
  */
 
-#ifndef __TRACKER_FTS_H__
-#define __TRACKER_FTS_H__
+#ifndef __LIBTRACKER_FTS_FTS_H__
+#define __LIBTRACKER_FTS_FTS_H__
 
 #include <sqlite3.h>
-#include <glib-object.h>
+
+#include <glib.h>
 
 G_BEGIN_DECLS
 
 gboolean    tracker_fts_init             (void);
+gboolean    tracker_fts_shutdown         (void);
 gboolean    tracker_fts_init_db          (sqlite3    *db,
                                           GHashTable *tables);
+gboolean    tracker_fts_shutdown_db      (sqlite3    *db);
 gboolean    tracker_fts_create_table     (sqlite3    *db,
                                           gchar      *table_name,
                                           GHashTable *tables,
@@ -42,5 +45,5 @@ gboolean    tracker_fts_alter_table      (sqlite3    *db,
 
 G_END_DECLS
 
-#endif /* __TRACKER_FTS_H__ */
+#endif /* __LIBTRACKER_FTS_FTS_H__ */
 


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