[gnumeric] Paste: fix leaks and Repeat command.



commit 1b20e333d3a3423fc5ced71a4ba80ff10513a00d
Author: Morten Welinder <terra gnome org>
Date:   Tue May 10 13:55:17 2011 -0400

    Paste: fix leaks and Repeat command.

 ChangeLog               |    7 +++++++
 NEWS                    |    1 +
 src/cmd-edit.c          |   14 +++++++-------
 src/commands.c          |   39 +++++++++++++++++++++------------------
 src/sheet-control-gui.c |    2 +-
 5 files changed, 37 insertions(+), 26 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 3452e75..b8a5248 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2011-05-10  Morten Welinder  <terra gnome org>
+
+	* src/commands.c (cmd_paste_copy): Clarify and fix ownership rules
+	for cell region.  Fixes leak.
+	(cmd_paste_copy_impl): Don't override the stored flags.  Fixes
+	paste-special repeat.
+
 2011-05-09  Morten Welinder  <terra gnome org>
 
 	* src/item-bar.c (item_bar_calc_size): Plug leak.
diff --git a/NEWS b/NEWS
index bac9547..4396890 100644
--- a/NEWS
+++ b/NEWS
@@ -22,6 +22,7 @@ Morten:
 	* Reduce memory usage when new glib is used.  [#644197]
 	* Eliminate col/row infos that are identical to the default.  [#155003]
 	* Fix problems reading some Lotus 1-2-3 files.
+	* Fix paste-special repeat.
 
 Urmas:
 	* Initial support for Works files.  [#647492]
diff --git a/src/cmd-edit.c b/src/cmd-edit.c
index c057b45..5d14a50 100644
--- a/src/cmd-edit.c
+++ b/src/cmd-edit.c
@@ -339,12 +339,13 @@ cmd_paste (WorkbookControl *wbc, GnmPasteTarget const *pt)
 			gnm_app_clipboard_clear (TRUE);
 
 	/* If this application has marked a selection use it */
-	} else if (content != NULL)
+	} else if (content != NULL) {
 		cmd_paste_copy (wbc, pt, content);
-
-	/* See if the control has access to information to paste */
-	else
+		/* We don't own the contents, so don't unref it.  */
+	} else {
+		/* See if the control has access to information to paste */
 		wb_control_paste_from_selection (wbc, pt);
+	}
 }
 
 /**
@@ -361,11 +362,10 @@ cmd_paste_to_selection (WorkbookControl *wbc, SheetView *dest_sv, int paste_flag
 	GnmRange const *r;
 	GnmPasteTarget pt;
 
-	if (!(r = selection_first_range (dest_sv, GO_CMD_CONTEXT (wbc), _("Paste"))))
+	r = selection_first_range (dest_sv, GO_CMD_CONTEXT (wbc), _("Paste"));
+	if (!r)
 		return;
 
-	g_return_if_fail (r !=NULL);
-
 	pt.sheet = dest_sv->sheet;
 	pt.range = *r;
 	pt.paste_flags = paste_flags;
diff --git a/src/commands.c b/src/commands.c
index 0a7f556..51c3ee6 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -2897,17 +2897,19 @@ static void
 cmd_paste_copy_repeat (GnmCommand const *cmd, WorkbookControl *wbc)
 {
 	CmdPasteCopy const *orig = (CmdPasteCopy const *) cmd;
-	GnmPasteTarget  new_dst;
+	GnmPasteTarget new_dst;
 	SheetView *sv = wb_control_cur_sheet_view (wbc);
 	GnmRange const	*r = selection_first_range (sv,
 		GO_CMD_CONTEXT (wbc), _("Paste Copy"));
+	GnmCellRegion *newcr;
 
 	if (r == NULL)
 		return;
 
 	paste_target_init (&new_dst, sv_sheet (sv), r, orig->dst.paste_flags);
-	cmd_paste_copy (wbc, &new_dst,
-		clipboard_copy_range (orig->dst.sheet, &orig->dst.range));
+	newcr = clipboard_copy_range (orig->dst.sheet, &orig->dst.range);
+	cmd_paste_copy (wbc, &new_dst, newcr);
+	cellregion_unref (newcr);
 }
 MAKE_GNM_COMMAND (CmdPasteCopy, cmd_paste_copy, cmd_paste_copy_repeat)
 
@@ -2964,6 +2966,7 @@ cmd_paste_copy_impl (GnmCommand *cmd, WorkbookControl *wbc,
 	CmdPasteCopy *me = CMD_PASTE_COPY (cmd);
 	GnmCellRegion *contents;
 	GSList *old_objects;
+	GnmPasteTarget actual_dst;
 
 	g_return_val_if_fail (me != NULL, TRUE);
 	g_return_val_if_fail (me->contents != NULL, TRUE);
@@ -2976,7 +2979,11 @@ cmd_paste_copy_impl (GnmCommand *cmd, WorkbookControl *wbc,
 	old_objects = get_new_objects (me->dst.sheet, NULL);
 
 	contents = clipboard_copy_range (me->dst.sheet, &me->dst.range);
-	if (clipboard_paste_region (me->contents, &me->dst, GO_CMD_CONTEXT (wbc))) {
+	actual_dst = me->dst;
+	if (me->has_been_through_cycle)
+		me->dst.paste_flags = PASTE_CONTENTS |
+			(me->dst.paste_flags & PASTE_ALL_TYPES);
+	if (clipboard_paste_region (me->contents, &actual_dst, GO_CMD_CONTEXT (wbc))) {
 		/* There was a problem, avoid leaking */
 		cellregion_unref (contents);
 		g_slist_free (old_objects);
@@ -2987,14 +2994,6 @@ cmd_paste_copy_impl (GnmCommand *cmd, WorkbookControl *wbc,
 	g_slist_foreach (me->pasted_objects, (GFunc)g_object_ref, NULL);
 	g_slist_free (old_objects);
 
-	if (me->has_been_through_cycle) {
-		cellregion_unref (me->contents);
-	} else {
-		/* Save the contents */
-		me->dst.paste_flags = PASTE_CONTENTS |
-			(me->dst.paste_flags & PASTE_ALL_TYPES);
-	}
-
 	if (is_undo) {
 		colrow_restore_state_group (me->dst.sheet, FALSE,
 			me->saved_list_rows, me->saved_sizes_rows);
@@ -3027,6 +3026,7 @@ cmd_paste_copy_impl (GnmCommand *cmd, WorkbookControl *wbc,
 				(GOMapFunc)sheet_object_dup)
 		: NULL;
 
+	cellregion_unref (me->contents);
 	me->contents = contents;
 	me->has_been_through_cycle = TRUE;
 
@@ -3061,17 +3061,14 @@ cmd_paste_copy_finalize (GObject *cmd)
 {
 	CmdPasteCopy *me = CMD_PASTE_COPY (cmd);
 
-	me->saved_sizes_rows = colrow_state_group_destroy
-		(me->saved_sizes_rows);
+	me->saved_sizes_rows = colrow_state_group_destroy (me->saved_sizes_rows);
 	colrow_index_list_destroy (me->saved_list_rows);
 	me->saved_list_rows = NULL;
-	me->saved_sizes_cols = colrow_state_group_destroy
-		(me->saved_sizes_cols);
+	me->saved_sizes_cols = colrow_state_group_destroy (me->saved_sizes_cols);
 	colrow_index_list_destroy (me->saved_list_cols);
 	me->saved_list_cols = NULL;
 	if (me->contents) {
-		if (me->has_been_through_cycle)
-			cellregion_unref (me->contents);
+		cellregion_unref (me->contents);
 		me->contents = NULL;
 	}
 	go_slist_free_custom (me->pasted_objects, (GFreeFunc)g_object_unref);
@@ -3079,6 +3076,9 @@ cmd_paste_copy_finalize (GObject *cmd)
 	gnm_command_finalize (cmd);
 }
 
+/*
+ * cmd_paste_copy will ref cr as needed.
+ */
 gboolean
 cmd_paste_copy (WorkbookControl *wbc,
 		GnmPasteTarget const *pt, GnmCellRegion *cr)
@@ -3090,6 +3090,9 @@ cmd_paste_copy (WorkbookControl *wbc,
 
 	g_return_val_if_fail (pt != NULL, TRUE);
 	g_return_val_if_fail (IS_SHEET (pt->sheet), TRUE);
+	g_return_val_if_fail (cr != NULL, TRUE);
+
+	cellregion_ref (cr);
 
 	me = g_object_new (CMD_PASTE_COPY_TYPE, NULL);
 
diff --git a/src/sheet-control-gui.c b/src/sheet-control-gui.c
index a920560..32e4cf7 100644
--- a/src/sheet-control-gui.c
+++ b/src/sheet-control-gui.c
@@ -3722,7 +3722,7 @@ scg_drag_receive_spreadsheet (SheetControlGUI *scg, const gchar *uri)
 
 static void
 scg_paste_cellregion (SheetControlGUI *scg, double x, double y,
-		       GnmCellRegion *content)
+		      GnmCellRegion *content)
 {
 	WorkbookControl	*wbc  = scg_wbc (scg);
 	Sheet *sheet = scg_sheet (scg) ;



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