Index: mail/em-utils.c
===================================================================
--- mail/em-utils.c	(revision 36683)
+++ mail/em-utils.c	(working copy)
@@ -2037,7 +2037,9 @@ try_open_e_book_cb (EBook *book, EBookSt
 {
 	struct TryOpenEBookStruct *data = (struct TryOpenEBookStruct *)closure;
 
-	if (!data)
+	/* If status is CANCELLED, it means try_open_e_book() has
+	 * already exited and the closure is pointing to garbage. */
+	if (status == E_BOOK_ERROR_CANCELLED || !data)
 		return;
 
 	data->result = status == E_BOOK_ERROR_OK;
@@ -2081,14 +2083,15 @@ try_open_e_book (EBook *book, gboolean o
 		e_flag_timed_wait (flag, &wait);
 	}
 
-	e_flag_free (flag);
-
 	if (canceled) {
 		g_set_error (error, E_BOOK_ERROR, E_BOOK_ERROR_CANCELLED, "Operation has been canceled.");
 		e_book_cancel_async_op (book, NULL);
+		e_flag_free (flag);
 		return FALSE;
 	}
 
+	e_flag_free (flag);
+
 	return data.result;
 }
 
@@ -2183,7 +2186,7 @@ em_utils_in_addressbook (CamelInternetAd
 			stop = err && err->domain == E_BOOK_ERROR && err->code == E_BOOK_ERROR_CANCELLED;
 			mail_cancel_hook_remove(hook);
 			g_object_unref(book);
-			d(g_warning("Can't get contacts: %s", err->message));
+			g_warning("Can't get contacts: %s", err->message);
 			g_clear_error(&err);
 			continue;
 		}
@@ -2267,7 +2270,7 @@ em_utils_contact_photo (struct _CamelInt
 		    || !e_book_get_contacts(book, query, &contacts, &err)) {
 			stop = err && err->domain == E_BOOK_ERROR && err->code == E_BOOK_ERROR_CANCELLED;
 			g_object_unref(book);
-			d(g_warning("Can't get contacts: %s", err->message));
+			g_warning("Can't get contacts: %s", err->message);
 			g_clear_error(&err);
 			continue;
 		}
