Index: camel/camel-store.c
===================================================================
--- camel/camel-store.c	(revision 9165)
+++ camel/camel-store.c	(working copy)
@@ -215,8 +215,11 @@ construct (CamelService *service, CamelS
 	store_db_path = g_build_filename (service->url->path, CAMEL_DB_FILE, NULL);
 
 	if (strlen (store_db_path) < 2) {
+		char *store_path = camel_session_get_storage_path (session, service, ex);
+
 		g_free (store_db_path);
-		store_db_path = g_build_filename ( camel_session_get_storage_path (session, service, ex), CAMEL_DB_FILE, NULL);		
+		store_db_path = g_build_filename (store_path, CAMEL_DB_FILE, NULL);
+		g_free (store_path);
 	}
 
 	store->cdb = camel_db_open (store_db_path, ex);
Index: camel/providers/imap/camel-imap-store.c
===================================================================
--- camel/providers/imap/camel-imap-store.c	(revision 9165)
+++ camel/providers/imap/camel-imap-store.c	(working copy)
@@ -210,6 +210,9 @@ camel_imap_store_finalize (CamelObject *
 		disco->diary = NULL;
 	}
 
+	g_free (imap_store->namespace);
+	imap_store->namespace = NULL;
+
 	g_free (imap_store->custom_headers);
 }
 
Index: camel/providers/imap/camel-imap-summary.c
===================================================================
--- camel/providers/imap/camel-imap-summary.c	(revision 9165)
+++ camel/providers/imap/camel-imap-summary.c	(working copy)
@@ -272,9 +272,11 @@ message_info_from_db (CamelFolderSummary
 
 	info = camel_imap_summary_parent->message_info_from_db (s, mir);
 	if (info) {
-		char *part = g_strdup (mir->bdata);
+		char *part = g_strdup (mir->bdata), *tmp;
+		tmp = part;
 		iinfo = (CamelImapMessageInfo *)info;
 		EXTRACT_FIRST_DIGIT (iinfo->server_flags)
+		g_free (tmp);
 	}
 
 	return info;
Index: camel/providers/imap/ChangeLog
===================================================================
--- camel/providers/imap/ChangeLog	(revision 9165)
+++ camel/providers/imap/ChangeLog	(working copy)
@@ -1,3 +1,10 @@
+2008-07-22  Milan Crha  <mcrha@redhat.com>
+
+	** Part of fix for bug #543943
+
+	* camel-imap-store.c: (camel_imap_store_finalize):
+	* camel-imap-summary.c: (message_info_from_db): Leak fix.
+
 2008-07-18  Matthew Barnes  <mbarnes@redhat.com>
 
 	* camel-imap-summary.c: #include "camel-string-utils.h"
Index: camel/providers/local/camel-local-summary.c
===================================================================
--- camel/providers/local/camel-local-summary.c	(revision 9165)
+++ camel/providers/local/camel-local-summary.c	(working copy)
@@ -597,7 +597,7 @@ static int 
 summary_header_from_db (CamelFolderSummary *s, CamelFIRecord *fir)
 {
 	CamelLocalSummary *cls = (CamelLocalSummary *)s;
-	char *part;
+	char *part, *tmp;
 
 	/* We dont actually add our own headers, but version that we don't anyway */
 
@@ -608,7 +608,11 @@ summary_header_from_db (CamelFolderSumma
 	if (part) {
 		EXTRACT_FIRST_DIGIT (cls->version)
 	}
-	fir->bdata = part;
+
+	/* keep only the rest of the bdata there (strip our version digit) */
+	tmp = g_strdup (part);
+	g_free (fir->bdata);
+	fir->bdata = tmp;
 
 	return 0;
 }
Index: camel/providers/local/ChangeLog
===================================================================
--- camel/providers/local/ChangeLog	(revision 9165)
+++ camel/providers/local/ChangeLog	(working copy)
@@ -1,3 +1,11 @@
+2008-07-22  Milan Crha  <mcrha@redhat.com>
+
+	** Part of fix for bug #543943
+
+	* camel-local-summary.c: (summary_header_from_db):
+	Reassign structure member correctly, otherwise can cause a crash.
+	* camel-maildir-store.c: (scan_dirs): Leak fix.
+
 2008-07-18  Matthew Barnes  <mbarnes@redhat.com>
 
 	* camel-local-summary.c: #include "camel-string-utils.h"
Index: camel/providers/local/camel-maildir-store.c
===================================================================
--- camel/providers/local/camel-maildir-store.c	(revision 9165)
+++ camel/providers/local/camel-maildir-store.c	(working copy)
@@ -488,6 +488,7 @@ scan_dirs(CamelStore *store, guint32 fla
 			g_free(tmp);
 		}
 		closedir(dir);
+		g_free (name);
 	}
 
 	res = 0;
Index: camel/camel-folder.c
===================================================================
--- camel/camel-folder.c	(revision 9165)
+++ camel/camel-folder.c	(working copy)
@@ -265,8 +265,11 @@ camel_folder_construct (CamelFolder *fol
 	store_db_path = g_build_filename (service->url->path, CAMEL_DB_FILE, NULL);
 	camel_exception_init(&ex);
 	if (strlen (store_db_path) < 2) {
+		char *store_path = camel_session_get_storage_path ((CamelSession *)camel_service_get_session (service), service, &ex);
+
 		g_free (store_db_path);
-		store_db_path = g_build_filename ( camel_session_get_storage_path ((CamelSession *)camel_service_get_session (service), service, &ex), CAMEL_DB_FILE, NULL);		
+		store_db_path = g_build_filename (store_path, CAMEL_DB_FILE, NULL);
+		g_free (store_path);
 	}
 
 
Index: camel/camel-vee-folder.c
===================================================================
--- camel/camel-vee-folder.c	(revision 9165)
+++ camel/camel-vee-folder.c	(working copy)
@@ -471,10 +471,10 @@ summary_header_to_db (CamelFolderSummary
 {
 	CamelFIRecord * record = g_new0 (CamelFIRecord, 1);
 	CamelDB *db;
+	char *table_name;
 
 	db = s->folder->parent_store->cdb;
-	char *table_name = s->folder->full_name;
-
+	table_name = s->folder->full_name;
 
 	record->folder_name = table_name;
 
Index: camel/camel.c
===================================================================
--- camel/camel.c	(revision 9165)
+++ camel/camel.c	(working copy)
@@ -105,6 +105,7 @@ camel_init (const char *configdir, gbool
 		if (NSS_InitReadWrite (nss_configdir) == SECFailure) {
 			/* fall back on using volatile dbs? */
 			if (NSS_NoDB_Init (nss_configdir) == SECFailure) {
+				g_free (nss_configdir);
 				g_warning ("Failed to initialize NSS");
 				return -1;
 			}
@@ -121,6 +122,8 @@ camel_init (const char *configdir, gbool
 		SSL_OptionSetDefault (SSL_ENABLE_SSL3, PR_TRUE);
 		SSL_OptionSetDefault (SSL_ENABLE_TLS, PR_TRUE);
 		SSL_OptionSetDefault (SSL_V2_COMPATIBLE_HELLO, PR_TRUE /* maybe? */);
+
+		g_free (nss_configdir);
 	}
 #endif /* HAVE_NSS */
 	
Index: camel/camel-db.c
===================================================================
--- camel/camel-db.c	(revision 9165)
+++ camel/camel-db.c	(working copy)
@@ -42,7 +42,7 @@ static GStaticRecMutex trans_lock = G_ST
 static int 
 cdb_sql_exec (sqlite3 *db, const char* stmt, CamelException *ex) 
 {
-	char *errmsg;
+	char *errmsg = NULL;
 	int   ret = -1;
 
 	d(g_print("Camel SQL Exec:\n%s\n", stmt));
@@ -50,16 +50,27 @@ cdb_sql_exec (sqlite3 *db, const char* s
 
 	ret = sqlite3_exec(db, stmt, 0, 0, &errmsg);
 	while (ret == SQLITE_BUSY || ret == SQLITE_LOCKED || ret == -1) {
-			ret = sqlite3_exec(db, stmt, 0, 0, &errmsg);
+		if (errmsg) {
+			sqlite3_free (errmsg);
+			errmsg = NULL;
+		}
+		ret = sqlite3_exec(db, stmt, 0, 0, &errmsg);
 	}
 
 	if (ret != SQLITE_OK) {
-			d(g_print ("Error in SQL EXEC statement: %s [%s].\n", stmt, errmsg));
-			if (ex)	
+		d(g_print ("Error in SQL EXEC statement: %s [%s].\n", stmt, errmsg));
+		if (ex)	
 			camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM, _(errmsg));
 		sqlite3_free (errmsg);
+		errmsg = NULL;
 		return -1;
 	}
+
+	if (errmsg) {
+		sqlite3_free (errmsg);
+		errmsg = NULL;
+	}
+
 	return 0;
 }
 
@@ -244,20 +255,32 @@ static int
 camel_db_count_message_info (CamelDB *cdb, const char *query, guint32 *count, CamelException *ex)
 {
 	int ret = -1;
-	char *errmsg;
+	char *errmsg = NULL;
 
 	ret = sqlite3_exec(cdb->db, query, count_cb, count, &errmsg);
 	while (ret == SQLITE_BUSY || ret == SQLITE_LOCKED) {
+		if (errmsg) {
+			sqlite3_free (errmsg);
+			errmsg = NULL;
+		}
+
 		ret = sqlite3_exec (cdb->db, query, count_cb, count, &errmsg);
 	}
 
 	CAMEL_DB_RELEASE_SQLITE_MEMORY;
 		
 	if (ret != SQLITE_OK) {
-			g_print ("Error in SQL SELECT statement: %s [%s]\n", query, errmsg);
-			camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM, _(errmsg));
-			sqlite3_free (errmsg);
+		g_print ("Error in SQL SELECT statement: %s [%s]\n", query, errmsg);
+		camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM, _(errmsg));
+		sqlite3_free (errmsg);
+		errmsg = NULL;
+	}
+
+	if (errmsg) {
+		sqlite3_free (errmsg);
+		errmsg = NULL;
 	}
+
 	return ret;
 }
 
@@ -385,7 +408,7 @@ camel_db_count_total_message_info (Camel
 int
 camel_db_select (CamelDB *cdb, const char* stmt, CamelDBSelectCB callback, gpointer data, CamelException *ex) 
 {
-  	char *errmsg;
+  	char *errmsg = NULL;
   	//int nrecs = 0;
 	int ret = -1;
 
@@ -396,6 +419,11 @@ camel_db_select (CamelDB *cdb, const cha
 
 	ret = sqlite3_exec(cdb->db, stmt, callback, data, &errmsg);
 	while (ret == SQLITE_BUSY || ret == SQLITE_LOCKED) {
+		if (errmsg) {
+			sqlite3_free (errmsg);
+			errmsg = NULL;
+		}
+
 		ret = sqlite3_exec (cdb->db, stmt, callback, data, &errmsg);
 	}
 
@@ -405,8 +433,14 @@ camel_db_select (CamelDB *cdb, const cha
     		d(g_warning ("Error in select statement '%s' [%s].\n", stmt, errmsg));
 		camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM, errmsg);
 		sqlite3_free (errmsg);
+		errmsg = NULL;
   	}
 
+	if (errmsg) {
+		sqlite3_free (errmsg);
+		errmsg = NULL;
+	}
+
 	return ret;
 }
 
Index: camel/ChangeLog
===================================================================
--- camel/ChangeLog	(revision 9165)
+++ camel/ChangeLog	(working copy)
@@ -1,3 +1,19 @@
+2008-07-22  Milan Crha  <mcrha@redhat.com>
+
+	** Fix for bug #543943
+
+	* camel-store.c: (construct):
+	* camel-folder.c: (camel_folder_construct):
+	* camel-vee-folder.c: (summary_header_to_db):
+	* camel.c: (camel_init):
+	* camel-db.c: (cdb_sql_exec), (camel_db_count_message_info),
+	(camel_db_select):
+	* camel-folder-summary.c: (camel_folder_summary_migrate_infos),
+	(message_info_from_uid), (camel_folder_summary_save_to_db),
+	(camel_folder_summary_header_load_from_db),
+	(camel_folder_summary_remove_range), (summary_header_to_db):
+	Leak fix.
+
 2008-07-22  Sankar P  <psankar@novell.com>
 
 	* camel-folder-search.c (check_header), (search_system_flag):
Index: camel/camel-folder-summary.c
===================================================================
--- camel/camel-folder-summary.c	(revision 9165)
+++ camel/camel-folder-summary.c	(working copy)
@@ -510,11 +510,14 @@ message_info_from_uid (CamelFolderSummar
 		info = g_hash_table_lookup (s->loaded_infos, uid);
 		
 		if (!info) {
+			char *errmsg = g_strdup_printf ("no uid [%s] exists", uid);
+
 			/* Makes no sense now as the exception is local as of now. FIXME: Pass exception from caller */
-			camel_exception_set (&ex, CAMEL_EXCEPTION_SYSTEM, _(g_strdup_printf ("no uid [%s] exists", uid)));
+			camel_exception_set (&ex, CAMEL_EXCEPTION_SYSTEM, _(errmsg));
 			// if (strcmp (folder_name, "UNMATCHED"))			
 			g_warning ("No uid[%s] exists in %s\n", uid, folder_name);
 			camel_exception_clear (&ex);
+			g_free (errmsg);
 		}
 	} else
 		info->refcount++;
@@ -1107,6 +1110,8 @@ camel_folder_summary_migrate_infos(Camel
 	}
 	
 	ret = camel_db_write_folder_info_record (cdb, record, &ex);
+
+	g_free (record->bdata);
 	g_free (record);
 
 	if (ret != 0) {
@@ -1251,6 +1256,7 @@ camel_folder_summary_save_to_db (CamelFo
 	
 	camel_db_begin_transaction (cdb, ex);
 	ret = camel_db_write_folder_info_record (cdb, record, ex);
+	g_free (record->bdata);
 	g_free (record);
 
 	if (ret != 0) {
@@ -1428,8 +1434,7 @@ camel_folder_summary_header_load_from_db
 	}
 
 	g_free (record->folder_name);
-	#warning "Crashes, take care"
-	//g_free (record->bdata); 
+	g_free (record->bdata);
 	g_free (record);
 
 	return ret;
@@ -2093,6 +2098,7 @@ camel_folder_summary_remove_range (Camel
 
 		CAMEL_SUMMARY_UNLOCK(s, summary_lock);
 
+		camel_exception_clear (&ex);
 	} else {
 		CAMEL_SUMMARY_UNLOCK(s, summary_lock);
 	}
@@ -2382,10 +2388,11 @@ summary_header_to_db (CamelFolderSummary
 {
 	CamelFIRecord * record = g_new0 (CamelFIRecord, 1);
 	CamelDB *db;
+	char *table_name;
 
 	db = s->folder->cdb;
-	//char *table_name = safe_table (camel_file_util_safe_filename (s->folder->full_name));
-	char *table_name = s->folder->full_name;
+	//table_name = safe_table (camel_file_util_safe_filename (s->folder->full_name));
+	table_name = s->folder->full_name;
 
 	io(printf("Savining header to db\n"));
 
