diff --git a/camel/camel-folder-search.c b/camel/camel-folder-search.c
index 5d71725..4cd08f5 100644
--- a/camel/camel-folder-search.c
+++ b/camel/camel-folder-search.c
@@ -266,6 +266,8 @@ void
 camel_folder_search_set_folder (CamelFolderSearch *search,
                                 CamelFolder *folder)
 {
+	g_return_if_fail (search != NULL);
+
 	search->folder = folder;
 }
 
@@ -283,6 +285,8 @@ void
 camel_folder_search_set_summary (CamelFolderSearch *search,
                                  GPtrArray *summary)
 {
+	g_return_if_fail (search != NULL);
+
 	search->summary = summary;
 }
 
@@ -300,6 +304,8 @@ void
 camel_folder_search_set_body_index (CamelFolderSearch *search,
                                     CamelIndex *index)
 {
+	g_return_if_fail (search != NULL);
+
 	if (search->body_index)
 		g_object_unref (search->body_index);
 	search->body_index = index;
@@ -334,8 +340,11 @@ camel_folder_search_execute_expression (CamelFolderSearch *search,
 	GPtrArray *matches;
 	gint i;
 	GHashTable *results;
-	CamelFolderSearchPrivate *p = search->priv;
+	CamelFolderSearchPrivate *p;
+
+	g_return_val_if_fail (search != NULL, NULL);
 
+	p = search->priv;
 	p->error = error;
 
 	/* only re-parse if the search has changed */
@@ -436,7 +445,11 @@ camel_folder_search_count (CamelFolderSearch *search,
 	GHashTable *results;
 	guint32 count = 0;
 
-	CamelFolderSearchPrivate *p = search->priv;
+	CamelFolderSearchPrivate *p;
+
+	g_return_val_if_fail (search != NULL, 0);
+
+	p = search->priv;
 
 	g_assert (search->folder);
 
@@ -595,7 +608,11 @@ camel_folder_search_search (CamelFolderSearch *search,
 	gchar *sql_query, *tmp, *tmp1;
 	GHashTable *results;
 
-	CamelFolderSearchPrivate *p = search->priv;
+	CamelFolderSearchPrivate *p;
+
+	g_return_val_if_fail (search != NULL, NULL);
+
+	p = search->priv;
 
 	g_assert (search->folder);
 
diff --git a/camel/camel-object-bag.c b/camel/camel-object-bag.c
index 79ca4a2..b369dd4 100644
--- a/camel/camel-object-bag.c
+++ b/camel/camel-object-bag.c
@@ -87,28 +87,36 @@ key_reservation_free (CamelObjectBag *bag,
 }
 
 static void
-object_bag_notify (CamelObjectBag *bag,
-                   GObject *where_the_object_was)
+object_bag_toggle_notify (gpointer user_data,
+			  GObject *object,
+			  gboolean is_last_ref)
 {
+	CamelObjectBag *bag = user_data;
 	gpointer key;
 
-	g_mutex_lock (bag->mutex);
+	if (is_last_ref) {
+		g_mutex_lock (bag->mutex);
 
-	key = g_hash_table_lookup (bag->key_table, where_the_object_was);
-	if (key != NULL) {
-		g_hash_table_remove (bag->key_table, where_the_object_was);
-		g_hash_table_remove (bag->object_table, key);
-	}
+		/* first remove from bag */
+		key = g_hash_table_lookup (bag->key_table, object);
+		if (key != NULL) {
+			g_hash_table_remove (bag->key_table, object);
+			g_hash_table_remove (bag->object_table, key);
+		}
 
-	g_mutex_unlock (bag->mutex);
+		g_mutex_unlock (bag->mutex);
+
+		/* then free the object */
+		g_object_remove_toggle_ref (object, object_bag_toggle_notify, bag);
+	}
 }
 
 static void
-object_bag_weak_unref (gpointer key,
-                       GObject *object,
-                       CamelObjectBag *bag)
+object_bag_toggle_unref (gpointer key,
+			 GObject *object,
+			 CamelObjectBag *bag)
 {
-	g_object_weak_unref (object, (GWeakNotify) object_bag_notify, bag);
+	g_object_remove_toggle_ref (object, object_bag_toggle_notify, bag);
 }
 
 static void
@@ -362,9 +370,8 @@ camel_object_bag_add (CamelObjectBag *bag,
 		g_hash_table_insert (bag->object_table, copied_key, object);
 		object_bag_unreserve (bag, key);
 
-		g_object_weak_ref (
-			G_OBJECT (object), (GWeakNotify)
-			object_bag_notify, bag);
+		g_object_add_toggle_ref (G_OBJECT (object),
+			object_bag_toggle_notify, bag);
 	}
 
 	g_mutex_unlock (bag->mutex);
@@ -492,7 +499,7 @@ camel_object_bag_remove (CamelObjectBag *bag,
 
 	key = g_hash_table_lookup (bag->key_table, object);
 	if (key != NULL) {
-		object_bag_weak_unref (key, object, bag);
+		object_bag_toggle_unref (key, object, bag);
 		g_hash_table_remove (bag->key_table, object);
 		g_hash_table_remove (bag->object_table, key);
 	}
@@ -513,10 +520,10 @@ camel_object_bag_destroy (CamelObjectBag *bag)
 	g_return_if_fail (bag != NULL);
 	g_return_if_fail (bag->reserved == NULL);
 
-	/* Drop remaining weak references. */
+	/* Drop remaining toggle references. */
 	g_hash_table_foreach (
 		bag->object_table, (GHFunc)
-		object_bag_weak_unref, bag);
+		object_bag_toggle_unref, bag);
 
 	g_hash_table_destroy (bag->key_table);
 	g_hash_table_destroy (bag->object_table);
