]> git.notmuchmail.org Git - notmuch/blobdiff - lib/database.cc
lib: Release resources if notmuch_database_open fails
[notmuch] / lib / database.cc
index d11dfaf3dc7245b6b0f7988f5056144e318d8dd4..94022d7ce3ae9c060ecf18baaca1aa95afa11781 100644 (file)
@@ -28,6 +28,8 @@
 #include <glib.h> /* g_free, GPtrArray, GHashTable */
 #include <glib-object.h> /* g_type_init */
 
+#include <gmime/gmime.h> /* g_mime_init */
+
 using namespace std;
 
 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
@@ -585,6 +587,7 @@ notmuch_database_open (const char *path,
     struct stat st;
     int err;
     unsigned int i, version;
+    static int initialized = 0;
 
     if (asprintf (&notmuch_path, "%s/%s", path, ".notmuch") == -1) {
        notmuch_path = NULL;
@@ -608,7 +611,13 @@ notmuch_database_open (const char *path,
     /* Initialize the GLib type system and threads */
     g_type_init ();
 
-    notmuch = talloc (NULL, notmuch_database_t);
+    /* Initialize gmime */
+    if (! initialized) {
+       g_mime_init (0);
+       initialized = 1;
+    }
+
+    notmuch = talloc_zero (NULL, notmuch_database_t);
     notmuch->exception_reported = FALSE;
     notmuch->path = talloc_strdup (notmuch, path);
 
@@ -694,6 +703,7 @@ notmuch_database_open (const char *path,
     } catch (const Xapian::Error &error) {
        fprintf (stderr, "A Xapian exception occurred opening database: %s\n",
                 error.get_msg().c_str());
+       notmuch_database_close (notmuch);
        notmuch = NULL;
     }