Index: gtksourceview/gtksourceundomanager.c
===================================================================
RCS file: /cvs/gnome/gtksourceview/gtksourceview/gtksourceundomanager.c,v
retrieving revision 1.8
diff -u -p -r1.8 gtksourceundomanager.c
--- gtksourceview/gtksourceundomanager.c	3 Feb 2005 17:55:16 -0000	1.8
+++ gtksourceview/gtksourceundomanager.c	28 Sep 2005 09:33:20 -0000
@@ -266,17 +266,27 @@ gtk_source_undo_manager_finalize (GObjec
 		gtk_source_undo_manager_free_action_list (um);
 	}
 
-	g_signal_handlers_disconnect_by_func (G_OBJECT (um->priv->document),
-			  G_CALLBACK (gtk_source_undo_manager_delete_range_handler), 
-			  um);
-
-	g_signal_handlers_disconnect_by_func (G_OBJECT (um->priv->document),
-			  G_CALLBACK (gtk_source_undo_manager_insert_text_handler), 
-			  um);
+	if (um->priv->document)
+	{
+		g_object_remove_weak_pointer (G_OBJECT (um->priv->document),
+				(gpointer*) &um->priv->document);
+		
+		g_signal_handlers_disconnect_by_func (G_OBJECT (um->priv->document),
+				G_CALLBACK (gtk_source_undo_manager_delete_range_handler), 
+				um);
+
+		g_signal_handlers_disconnect_by_func (G_OBJECT (um->priv->document),
+				G_CALLBACK (gtk_source_undo_manager_insert_text_handler), 
+				um);
 	
-	g_signal_handlers_disconnect_by_func (G_OBJECT (um->priv->document),
-			  G_CALLBACK (gtk_source_undo_manager_begin_user_action_handler), 
-			  um);
+		g_signal_handlers_disconnect_by_func (G_OBJECT (um->priv->document),
+				G_CALLBACK (gtk_source_undo_manager_begin_user_action_handler), 
+				um);
+
+		g_signal_handlers_disconnect_by_func (G_OBJECT (um->priv->document),
+				G_CALLBACK (gtk_source_undo_manager_modified_changed_handler),
+				um);
+	}
 
 	g_free (um->priv);
 
@@ -288,10 +298,14 @@ gtk_source_undo_manager_new (GtkTextBuff
 {
  	GtkSourceUndoManager *um;
 
+	g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
+	
 	um = GTK_SOURCE_UNDO_MANAGER (g_object_new (GTK_SOURCE_TYPE_UNDO_MANAGER, NULL));
 
 	g_return_val_if_fail (um->priv != NULL, NULL);
-  	um->priv->document = buffer;
+  	
+	um->priv->document = buffer;
+	g_object_add_weak_pointer (G_OBJECT (buffer), (gpointer*) &um->priv->document);
 
 	g_signal_connect (G_OBJECT (buffer), "insert_text",
 			  G_CALLBACK (gtk_source_undo_manager_insert_text_handler), 
@@ -308,6 +322,7 @@ gtk_source_undo_manager_new (GtkTextBuff
 	g_signal_connect (G_OBJECT (buffer), "modified_changed",
 			  G_CALLBACK (gtk_source_undo_manager_modified_changed_handler),
 			  um);
+	
 	return um;
 }
 
