--- gtksourceview-3.0.1.orig/gtksourceview/gtksourcestylescheme.c	2011-03-20 20:40:41.000000000 +0000
+++ gtksourceview-3.0.1/gtksourceview/gtksourcestylescheme.c	2011-05-15 19:52:55.000000000 +0100
@@ -688,13 +688,11 @@ remove_generated_css (GtkSourceStyleSche
 		                                   GTK_STYLE_PROVIDER (scheme->priv->css));
 
 		g_object_unref (scheme->priv->css);
-		scheme->priv->css = NULL;
 	}
 }
 
 static void
-generate_css_style (GtkSourceStyleScheme *scheme,
-                    GtkWidget            *widget)
+generate_css_style (GtkSourceStyleScheme *scheme)
 {
 	GString *final_style;
 	GtkSourceStyle *style, *style2;
@@ -712,28 +710,20 @@ generate_css_style (GtkSourceStyleScheme
 		style2 = style;
 	append_css_style (final_style, style2, ":selected");
 
-	/* if there is a previously applied css remove it first */
-	remove_generated_css (scheme, widget);
-
 	if (*final_style->str != '\0')
 	{
 		GError *error = NULL;
 
 		scheme->priv->css = gtk_css_provider_new ();
-		if (gtk_css_provider_load_from_data (scheme->priv->css, final_style->str,
+		
+		g_object_add_weak_pointer (G_OBJECT(scheme->priv->css), (gpointer) &scheme->priv->css);
+		
+		if (!gtk_css_provider_load_from_data (scheme->priv->css, final_style->str,
 		                                     final_style->len, &error))
 		{
-			GtkStyleContext *context;
-
-			context = gtk_widget_get_style_context (GTK_WIDGET (widget));
-			gtk_style_context_add_provider (context,
-			                                GTK_STYLE_PROVIDER (scheme->priv->css),
-			                                GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
-		}
-		else
-		{
 			g_warning ("%s", error->message);
 			g_error_free (error);
+			g_object_unref (scheme->priv->css);
 		}
 	}
 
@@ -741,6 +731,29 @@ generate_css_style (GtkSourceStyleScheme
 }
 
 static void
+apply_css_style (GtkSourceStyleScheme *scheme,
+                 GtkWidget            *widget)
+{
+	if (scheme->priv->css)
+	{
+		g_object_ref (scheme->priv->css);
+	}
+	else
+	{
+		generate_css_style (scheme);
+		if (!scheme->priv->css)
+			return;
+	}
+	
+	GtkStyleContext *context;
+
+	context = gtk_widget_get_style_context (GTK_WIDGET (widget));
+	gtk_style_context_add_provider (context,
+	                                GTK_STYLE_PROVIDER (scheme->priv->css),
+	                                GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+}
+
+static void
 set_line_numbers_style (GtkWidget      *widget,
 			GtkSourceStyle *style)
 {
@@ -820,7 +833,7 @@ _gtk_source_style_scheme_apply (GtkSourc
 	g_return_if_fail (GTK_IS_WIDGET (widget));
 
 	/* we need to translate some of the style scheme properties in a CSS override */
-	generate_css_style (scheme, widget);
+	apply_css_style (scheme, widget);
 
 	style = gtk_source_style_scheme_get_style (scheme, STYLE_LINE_NUMBERS);
 	set_line_numbers_style (widget, style);
