Index: mail/mail-stub-exchange.c
===================================================================
--- mail/mail-stub-exchange.c	(revision 1340)
+++ mail/mail-stub-exchange.c	(working copy)
@@ -43,7 +43,7 @@
 #include <exchange-hierarchy.h>
 #include <mapi.h>
 
-#define d(x)
+#define d(x) (x)
 
 #define PARENT_TYPE MAIL_TYPE_STUB
 static MailStubClass *parent_class = NULL;
@@ -134,6 +134,9 @@
 static void folder_update_linestatus (gpointer key, gpointer value, gpointer data);
 static void free_folder (gpointer value);
 static gboolean get_folder_online (MailStubExchangeFolder *mfld, gboolean background);
+
+static GStaticMutex g_changed_msgs_mutex = G_STATIC_MUTEX_INIT;
+
 static void
 class_init (GObjectClass *object_class)
 {
@@ -190,9 +193,13 @@
 	MailStubExchangeFolder *mfld = value;
 	int i;
 
+	d(g_print ("%s:%s:%d: freeing mfld: name=[%s]\n", __FILE__, __PRETTY_FUNCTION__, __LINE__,
+		   mfld->name));
+
 	e_folder_exchange_unsubscribe (mfld->folder);
 	g_signal_handlers_disconnect_by_func (mfld->folder, storage_folder_changed, mfld);
 	g_object_unref (mfld->folder);
+	mfld->folder = NULL;
 
 	for (i = 0; i < mfld->messages->len; i++)
 		free_message (mfld->messages->pdata[i]);
@@ -360,7 +367,9 @@
 
 		for (i = 0; i < mfld->changed_messages->len; i++) {
 			if (mfld->changed_messages->pdata[i] == (gpointer)mmsg) {
+				g_static_rec_mutex_lock (&g_changed_msgs_mutex);
 				g_ptr_array_remove_index_fast (mfld->changed_messages, i);
+				g_static_rec_mutex_unlock (&g_changed_msgs_mutex);
 				break;
 			}
 		}
@@ -1610,6 +1619,13 @@
 		g_warning ("update_tags: %d", status);
 }
 
+static void dump_tag_update_values (GQuark key_id, gpointer data, 
+				    gpointer user_data)
+{
+	d(g_print ("Dumping tag_update_values: \n"));
+	d(g_print ("Key=[%s], value=[%s]\n", g_quark_to_string (key_id), data ? (char *)data:"{NULL}"));
+}
+
 static gboolean
 process_flags (gpointer user_data)
 {
@@ -1620,8 +1636,16 @@
 	int i;
 	guint32 hier_type = e_folder_exchange_get_hierarchy (mfld->folder)->type;
 
+	g_static_rec_mutex_lock (&g_changed_msgs_mutex);
+
 	for (i = 0; i < mfld->changed_messages->len; i++) {
 		mmsg = mfld->changed_messages->pdata[i];
+		
+		if (!mmsg->href) {
+			d(g_print ("%s:%s:%d: mfld = [%s], type=[%d]\n", __FILE__, __PRETTY_FUNCTION__,
+				   __LINE__, mfld->name, mfld->type));
+			g_datalist_foreach (&mmsg->tag_updates, dump_tag_update_values, NULL);
+		}
 
 		if (mmsg->change_mask & MAIL_STUB_MESSAGE_SEEN) {
 			if (mmsg->change_flags & MAIL_STUB_MESSAGE_SEEN) {
@@ -1655,6 +1679,8 @@
 			g_ptr_array_remove_index_fast (mfld->changed_messages, i--);
 	}
 
+	g_static_rec_mutex_unlock (&g_changed_msgs_mutex);
+
 	if (seen || unseen) {
 		if (seen) {
 			mark_read (mfld->folder, seen, TRUE);
@@ -1672,6 +1698,8 @@
 		} else
 			return TRUE;
 	}
+	
+	g_static_rec_mutex_lock (&g_changed_msgs_mutex);
 
 	for (i = 0; i < mfld->changed_messages->len; i++) {
 		mmsg = mfld->changed_messages->pdata[i];
@@ -1681,6 +1709,7 @@
 			g_ptr_array_add (deleted, strrchr (mmsg->href, '/') + 1);
 		}
 	}
+	g_static_rec_mutex_unlock (&g_changed_msgs_mutex);
 
 	if (deleted) {
 		MailStub *stub = MAIL_STUB (mse);
@@ -2295,7 +2324,7 @@
 	exchange_account_is_offline (mse->account, &mode);
 	if (!subscribed) {
 		ExchangeAccountResult result = -1;
-		d(g_print ("%s(%d):%s: NOT SUBSCRIBED top = [%s]\n", __FILE__, __LINE__, __GNUC_PRETTY_FUNCTION__, top));
+		d(g_print ("%s(%d):%s: NOT SUBSCRIBED top = [%s]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, top));
 		if (!toplen)
 			result = exchange_account_open_folder (mse->account, "/public");
 		if (result ==  EXCHANGE_ACCOUNT_FOLDER_DOES_NOT_EXIST) {
@@ -2303,27 +2332,17 @@
 			if (hier)
 				exchange_hierarchy_scan_subtree (hier, hier->toplevel, mode);
 		} else
-			d(g_print ("%s(%d):%s: NOT SUBSCRIBED - open_folder returned = [%d]\n", __FILE__, __LINE__, __GNUC_PRETTY_FUNCTION__, result));
+			d(g_print ("%s(%d):%s: NOT SUBSCRIBED - open_folder returned = [%d]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, result));
 	}
 
 	if (!recursive && toplen) {
-		d(g_print ("%s(%d):%s: NOT RECURSIVE and toplen top = [%s]\n", __FILE__, __LINE__, __GNUC_PRETTY_FUNCTION__, top));
+		d(g_print ("%s(%d):%s: NOT RECURSIVE and toplen top = [%s]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, top));
 		full_path = g_strdup_printf ("/%s", top);
 		folders = exchange_account_get_folder_tree (mse->account, full_path);
-			/*
-			type = e_folder_get_type_string (folder);
-			if ((!strcmp (type, "mail")) || 
-				(!strcmp (type, "mail/public"))) {
-				d(g_print ("%s(%d):%s: Setting single_folder = TRUE\n", __FILE__, __LINE__, __PRETTY_FUNCTION__));
-				folders = g_ptr_array_new ();
-				g_ptr_array_add (folders, folder);
-				single_folder = TRUE;
-			}
-			*/
 		g_free (full_path);
 	} else {
 		d(g_print ("%s(%d):%s calling exchange_account_get_folders \n", __FILE__, __LINE__,
-					   __GNUC_PRETTY_FUNCTION__));
+					   __PRETTY_FUNCTION__));
 		folders = exchange_account_get_folders (mse->account);
 	}
 
@@ -2406,7 +2425,7 @@
 				folder_flags |= CAMEL_STUB_FOLDER_SYSTEM|CAMEL_STUB_FOLDER_TYPE_INBOX;
 
 			if (!e_folder_exchange_get_has_subfolders (folder)) {
-				d(printf ("%s:%d:%s - %s has no subfolders", __FILE__, __LINE__, __GNUC_PRETTY_FUNCTION__,
+				d(printf ("%s:%d:%s - %s has no subfolders", __FILE__, __LINE__, __PRETTY_FUNCTION__,
 					  name));
 				folder_flags |= CAMEL_STUB_FOLDER_NOCHILDREN;
 			}
