From 157ab6f9668ff9c10bb9a67ab698f9b7364cbfcf Mon Sep 17 00:00:00 2001
From: Ivica Ico Bukvic <ico@vt.edu>
Date: Fri, 23 May 2014 20:00:46 -0400
Subject: [PATCH] *fixed segfault when trying to duplicate an uninitited object
 that we just typed into.

---
 pd/src/g_editor.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/pd/src/g_editor.c b/pd/src/g_editor.c
index 380b73b71..918b33e7c 100644
--- a/pd/src/g_editor.c
+++ b/pd/src/g_editor.c
@@ -5663,13 +5663,35 @@ static void canvas_duplicate(t_canvas *x)
 	//if (x->gl_editor->e_onmotion == MA_NONE && x->gl_editor->e_selection)
 	if (x->gl_editor->e_onmotion == MA_NONE && c_selection && c_selection->gl_editor->e_selection)
     {
+    	// Check if we are trying to duplicate an object that we just typed in and which has not been
+    	// instantiated yet. If so, let's deselect it to instatiate it and then reselect it again.
+    	// If this is the case, only one object will be selected. We temporarily borrow g object for
+    	// this operation.
+    	t_gobj *g;
+
+    	g = c_selection->gl_editor->e_selection->sel_what;
+    	if (!c_selection->gl_editor->e_selection->sel_next &&
+    			c_selection->gl_editor->e_textedfor &&
+    			pd_class(&g->g_pd) == text_class &&
+    			((t_text *)x)->te_type == T_OBJECT)
+    	{
+    		//fprintf(stderr,"got uninitiated object we are trying to duplicate...\n");
+    		glist_deselect(x, g);
+    		// now we need to find the newly instantiated object and reselect it
+			g = x->gl_list;
+			if (g)
+				while (g->g_next)
+					g = g->g_next;
+
+    	}
+
         //canvas_copy(x);
         //canvas_setundo(x, canvas_undo_paste, canvas_undo_set_paste(x),
         //    "duplicate");
         //canvas_dopaste(x, copy_binbuf);
         //canvas_paste_xyoffset(x);
         //canvas_dirty(x, 1);
-		t_gobj *g = x->gl_list;
+		g = x->gl_list;
 		if (g)
 			while (g->g_next)
 				g = g->g_next;
-- 
GitLab