[gedit] Do not leak GFiles in the filebrowser plugin.



commit e2f792359a53db544824b4a113427374841f228a
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Mar 20 18:40:32 2011 +0100

    Do not leak GFiles in the filebrowser plugin.
    
    Retrieving GFiles from the the treemodel returns a new ref that must be
    unreffed properly!

 plugins/filebrowser/gedit-file-browser-messages.c |   35 ++++++-----
 plugins/filebrowser/gedit-file-browser-plugin.c   |   62 +++++++++++++-------
 plugins/filebrowser/gedit-file-browser-store.c    |    1 +
 plugins/filebrowser/gedit-file-browser-view.c     |   43 ++++++++++-----
 plugins/filebrowser/gedit-file-browser-widget.c   |   21 +++++--
 5 files changed, 104 insertions(+), 58 deletions(-)
---
diff --git a/plugins/filebrowser/gedit-file-browser-messages.c b/plugins/filebrowser/gedit-file-browser-messages.c
index 702a91c..0695bf6 100644
--- a/plugins/filebrowser/gedit-file-browser-messages.c
+++ b/plugins/filebrowser/gedit-file-browser-messages.c
@@ -340,34 +340,35 @@ set_item_message (WindowData   *data,
 	GeditFileBrowserStore *store;
 	GFile *location;
 	guint flags = 0;
-	gchar *track_id;
 
 	store = gedit_file_browser_widget_get_browser_store (data->widget);
-
 	gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
 			    GEDIT_FILE_BROWSER_STORE_COLUMN_LOCATION, &location,
 			    GEDIT_FILE_BROWSER_STORE_COLUMN_FLAGS, &flags,
 			    -1);
 
-	if (!location)
-		return;
+	if (location)
+	{
+		gchar *track_id;
 
-	if (path && gtk_tree_path_get_depth (path) != 0)
-		track_id = track_row (data, store, path, location);
-	else
-		track_id = NULL;
+		if (path && gtk_tree_path_get_depth (path) != 0)
+			track_id = track_row (data, store, path, location);
+		else
+			track_id = NULL;
 
-	gedit_message_set (message,
-			   "id", track_id,
-			   "location", location,
-			   NULL);
-
-	if (gedit_message_has_key (message, "is_directory"))
 		gedit_message_set (message,
-				   "is_directory", FILE_IS_DIR (flags),
+				   "id", track_id,
+				   "location", location,
 				   NULL);
 
-	g_free (track_id);
+		if (gedit_message_has_key (message, "is_directory"))
+			gedit_message_set (message,
+					   "is_directory", FILE_IS_DIR (flags),
+					   NULL);
+
+		g_free (track_id);
+		g_object_unref (location);
+	}
 }
 
 static gboolean
@@ -399,6 +400,8 @@ custom_message_filter_func (GeditFileBrowserWidget *widget,
 	gedit_message_bus_send_message_sync (wdata->bus, data->message);
 	gedit_message_get (data->message, "filter", &filter, NULL);
 
+	g_object_unref (location);
+
 	return !filter;
 }
 
diff --git a/plugins/filebrowser/gedit-file-browser-plugin.c b/plugins/filebrowser/gedit-file-browser-plugin.c
index 63058af..38419eb 100644
--- a/plugins/filebrowser/gedit-file-browser-plugin.c
+++ b/plugins/filebrowser/gedit-file-browser-plugin.c
@@ -487,9 +487,6 @@ on_action_open_terminal (GtkAction              *action,
                          GeditFileBrowserPlugin *plugin)
 {
 	GeditFileBrowserPluginPrivate *priv = plugin->priv;
-	gchar *terminal;
-	gchar *local;
-	gchar *argv[2];
 	GFile *file;
 
 	GtkTreeIter iter;
@@ -506,27 +503,32 @@ on_action_open_terminal (GtkAction              *action,
 	                    &file,
 	                    -1);
 
-	if (file == NULL)
-		return;
+	if (file)
+	{
+		gchar *terminal;
+		gchar *local;
+		gchar *argv[2];
 
-	terminal = get_terminal (plugin);
+		terminal = get_terminal (plugin);
 
-	local = g_file_get_path (file);
+		local = g_file_get_path (file);
 
-	argv[0] = terminal;
-	argv[1] = NULL;
+		argv[0] = terminal;
+		argv[1] = NULL;
 
-	g_spawn_async (local,
-	               argv,
-	               NULL,
-	               G_SPAWN_SEARCH_PATH,
-	               NULL,
-	               NULL,
-	               NULL,
-	               NULL);
+		g_spawn_async (local,
+			       argv,
+			       NULL,
+			       G_SPAWN_SEARCH_PATH,
+			       NULL,
+			       NULL,
+			       NULL,
+			       NULL);
 
-	g_free (terminal);
-	g_free (local);
+		g_free (terminal);
+		g_free (local);
+		g_object_unref (file);
+	}
 }
 
 static void
@@ -538,7 +540,6 @@ on_selection_changed_cb (GtkTreeSelection       *selection,
 	GtkTreeModel *model;
 	GtkTreeIter iter;
 	gboolean sensitive;
-	GFile *location;
 
 	tree_view = GTK_TREE_VIEW (gedit_file_browser_widget_get_browser_view (priv->tree_widget));
 	model = gtk_tree_view_get_model (tree_view);
@@ -550,11 +551,21 @@ on_selection_changed_cb (GtkTreeSelection       *selection,
 
 	if (sensitive)
 	{
+		GFile *location;
+
 		gtk_tree_model_get (model, &iter,
 				    GEDIT_FILE_BROWSER_STORE_COLUMN_LOCATION,
 				    &location, -1);
 
-		sensitive = gedit_utils_location_has_file_scheme (location);
+		if (location)
+		{
+			sensitive = gedit_utils_location_has_file_scheme (location);
+			g_object_unref (location);
+		}
+		else
+		{
+			sensitive = FALSE;
+		}
 	}
 
 	gtk_action_set_sensitive (
@@ -1083,13 +1094,20 @@ get_filename_from_path (GtkTreeModel *model,
 {
 	GtkTreeIter iter;
 	GFile *location;
+	gchar *ret = NULL;
 
 	gtk_tree_model_get_iter (model, &iter, path);
 	gtk_tree_model_get (model, &iter,
 			    GEDIT_FILE_BROWSER_STORE_COLUMN_LOCATION, &location,
 			    -1);
 
-	return gedit_file_browser_utils_file_basename (location);
+	if (location)
+	{
+		ret = gedit_file_browser_utils_file_basename (location);
+		g_object_unref (location);
+	}
+
+	return ret;
 }
 
 static gboolean
diff --git a/plugins/filebrowser/gedit-file-browser-store.c b/plugins/filebrowser/gedit-file-browser-store.c
index dc4c6a6..8bfcc29 100644
--- a/plugins/filebrowser/gedit-file-browser-store.c
+++ b/plugins/filebrowser/gedit-file-browser-store.c
@@ -985,6 +985,7 @@ gedit_file_browser_store_drag_data_get (GtkTreeDragSource *drag_source,
 	ret = gtk_selection_data_set_uris (selection_data, uris);
 
 	g_free (uris[0]);
+	g_object_unref (location);
 
 	return ret;
 }
diff --git a/plugins/filebrowser/gedit-file-browser-view.c b/plugins/filebrowser/gedit-file-browser-view.c
index f3658f0..68f8cf1 100644
--- a/plugins/filebrowser/gedit-file-browser-view.c
+++ b/plugins/filebrowser/gedit-file-browser-view.c
@@ -154,7 +154,6 @@ row_expanded (GtkTreeView *tree_view,
 	      GtkTreePath *path)
 {
 	GeditFileBrowserView *view = GEDIT_FILE_BROWSER_VIEW (tree_view);
-	GFile *location;
 
 	if (GTK_TREE_VIEW_CLASS (gedit_file_browser_view_parent_class)->row_expanded)
 		GTK_TREE_VIEW_CLASS (gedit_file_browser_view_parent_class)->row_expanded (tree_view, iter, path);
@@ -164,6 +163,8 @@ row_expanded (GtkTreeView *tree_view,
 
 	if (view->priv->restore_expand_state)
 	{
+		GFile *location;
+
 		gtk_tree_model_get (view->priv->model,
 				    iter,
 				    GEDIT_FILE_BROWSER_STORE_COLUMN_LOCATION,
@@ -171,6 +172,9 @@ row_expanded (GtkTreeView *tree_view,
 				    -1);
 
 		add_expand_state (view, location);
+
+		if (location)
+			g_object_unref (location);
 	}
 
 	_gedit_file_browser_store_iter_expanded (GEDIT_FILE_BROWSER_STORE (view->priv->model),
@@ -183,7 +187,6 @@ row_collapsed (GtkTreeView *tree_view,
 	       GtkTreePath *path)
 {
 	GeditFileBrowserView *view = GEDIT_FILE_BROWSER_VIEW (tree_view);
-	GFile *location;
 
 	if (GTK_TREE_VIEW_CLASS (gedit_file_browser_view_parent_class)->row_collapsed)
 		GTK_TREE_VIEW_CLASS (gedit_file_browser_view_parent_class)->row_collapsed (tree_view, iter, path);
@@ -193,6 +196,8 @@ row_collapsed (GtkTreeView *tree_view,
 
 	if (view->priv->restore_expand_state)
 	{
+		GFile *location;
+
 		gtk_tree_model_get (view->priv->model,
 				    iter,
 				    GEDIT_FILE_BROWSER_STORE_COLUMN_LOCATION,
@@ -200,6 +205,9 @@ row_collapsed (GtkTreeView *tree_view,
 				    -1);
 
 		remove_expand_state (view, location);
+
+		if (location)
+			g_object_unref (location);
 	}
 
 	_gedit_file_browser_store_iter_collapsed (GEDIT_FILE_BROWSER_STORE (view->priv->model),
@@ -711,7 +719,6 @@ fill_expand_state (GeditFileBrowserView *view,
 {
 	GtkTreePath *path;
 	GtkTreeIter child;
-	GFile *location;
 
 	if (!gtk_tree_model_iter_has_child (view->priv->model, iter))
 		return;
@@ -720,6 +727,8 @@ fill_expand_state (GeditFileBrowserView *view,
 
 	if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (view), path))
 	{
+		GFile *location;
+
 		gtk_tree_model_get (view->priv->model,
 				    iter,
 				    GEDIT_FILE_BROWSER_STORE_COLUMN_LOCATION,
@@ -727,6 +736,9 @@ fill_expand_state (GeditFileBrowserView *view,
 				    -1);
 
 		add_expand_state (view, location);
+
+		if (location)
+			g_object_unref (location);
 	}
 
 	if (gtk_tree_model_iter_children (view->priv->model, &child, iter))
@@ -1249,7 +1261,6 @@ restore_expand_state (GeditFileBrowserView  *view,
 		      GtkTreeIter           *iter)
 {
 	GFile *location;
-	GtkTreePath *path;
 
 	gtk_tree_model_get (GTK_TREE_MODEL (model),
 			    iter,
@@ -1257,19 +1268,23 @@ restore_expand_state (GeditFileBrowserView  *view,
 			    &location,
 			    -1);
 
-	if (!location)
-		return;
+	if (location)
+	{
+		GtkTreePath *path;
 
-	path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), iter);
+		path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), iter);
 
-	if (g_hash_table_lookup (view->priv->expand_state, location))
-	{
-		gtk_tree_view_expand_row (GTK_TREE_VIEW (view),
-					  path,
-					  FALSE);
-	}
+		if (g_hash_table_lookup (view->priv->expand_state, location))
+		{
+			gtk_tree_view_expand_row (GTK_TREE_VIEW (view),
+						  path,
+						  FALSE);
+		}
 
-	gtk_tree_path_free (path);
+		gtk_tree_path_free (path);
+
+		g_object_unref (location);
+	}
 }
 
 static void
diff --git a/plugins/filebrowser/gedit-file-browser-widget.c b/plugins/filebrowser/gedit-file-browser-widget.c
index 95a7b9a..4c713ec 100644
--- a/plugins/filebrowser/gedit-file-browser-widget.c
+++ b/plugins/filebrowser/gedit-file-browser-widget.c
@@ -2506,6 +2506,9 @@ file_open  (GeditFileBrowserWidget *obj,
 
 	if (!FILE_IS_DIR (flags) && !FILE_IS_DUMMY (flags))
 		g_signal_emit (obj, signals[LOCATION_ACTIVATED], 0, location);
+
+	if (location)
+		g_object_unref (location);
 }
 
 static gboolean
@@ -2541,6 +2544,7 @@ directory_open (GeditFileBrowserWidget *obj,
 		}
 
 		g_free (uri);
+		g_object_unref (location);
 	}
 
 	return result;
@@ -2588,11 +2592,6 @@ on_virtual_root_changed (GeditFileBrowserStore  *model,
 			 GeditFileBrowserWidget *obj)
 {
 	GtkTreeIter iter;
-	GFile *location;
-	GtkTreeIter root;
-	GtkAction *action;
-	Location *loc;
-	GdkPixbuf *pixbuf;
 
 	if (gtk_tree_view_get_model (GTK_TREE_VIEW (obj->priv->treeview)) !=
 	    GTK_TREE_MODEL (obj->priv->file_store))
@@ -2602,14 +2601,22 @@ on_virtual_root_changed (GeditFileBrowserStore  *model,
 
 	if (gedit_file_browser_store_get_iter_virtual_root (model, &iter))
 	{
+		GFile *location;
+		GtkTreeIter root;
+
 		gtk_tree_model_get (GTK_TREE_MODEL (model), &iter,
 				    GEDIT_FILE_BROWSER_STORE_COLUMN_LOCATION,
 				    &location, -1);
 
 		if (gedit_file_browser_store_get_iter_root (model, &root))
 		{
+			GtkAction *action;
+
 			if (!obj->priv->changing_location)
 			{
+				Location *loc;
+				GdkPixbuf *pixbuf;
+
 				/* Remove all items from obj->priv->current_location on */
 				if (obj->priv->current_location)
 					clear_next_locations (obj);
@@ -2648,7 +2655,6 @@ on_virtual_root_changed (GeditFileBrowserStore  *model,
 
 				if (pixbuf)
 					g_object_unref (pixbuf);
-
 			}
 
 			action = gtk_action_group_get_action (obj->priv->action_group,
@@ -2670,6 +2676,9 @@ on_virtual_root_changed (GeditFileBrowserStore  *model,
 		}
 
 		check_current_item (obj, TRUE);
+
+		if (location)
+			g_object_unref (location);
 	}
 	else
 	{



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]