From 03ef5d01861b2f50071a0ee189a0468f6c8dd9e6 Mon Sep 17 00:00:00 2001
From: Ivica Ico Bukvic <ico@vt.edu>
Date: Sat, 13 Sep 2014 01:01:02 -0400
Subject: [PATCH] *made undo+preset hub sync more robust *fixed preset segfault
 when doing clear on a hubless node *clean-up

---
 pd/src/g_text.c   |  1 -
 pd/src/g_undo.c   |  2 ++
 pd/src/x_preset.c | 11 +++++------
 3 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/pd/src/g_text.c b/pd/src/g_text.c
index 06fdf4937..67ea85a37 100644
--- a/pd/src/g_text.c
+++ b/pd/src/g_text.c
@@ -2384,7 +2384,6 @@ void text_setto(t_text *x, t_glist *glist, char *buf, int bufsize, int pos)
 
         //probably don't need this here, but doesn't hurt to leave it in
         glob_preset_node_list_seek_hub();
-
         glob_preset_node_list_check_loc_and_update();
     }
 }
diff --git a/pd/src/g_undo.c b/pd/src/g_undo.c
index 9083bdfee..6356b7841 100644
--- a/pd/src/g_undo.c
+++ b/pd/src/g_undo.c
@@ -89,6 +89,7 @@ void canvas_undo_undo(t_canvas *x)
            their regular call will fail in case their position needed
            to be updated by undo/redo first to reflect the old one */
         glob_preset_node_list_seek_hub();
+        glob_preset_node_list_check_loc_and_update();
         if (glist_isvisible(x) && glist_istoplevel(x))
         {
             sys_vgui("pdtk_undomenu .x%lx %s %s\n",
@@ -133,6 +134,7 @@ void canvas_undo_redo(t_canvas *x)
            regular call will fail in case their position needed to be updated
            by undo/redo first to reflect the old one */
         glob_preset_node_list_seek_hub();
+        glob_preset_node_list_check_loc_and_update();
         if (glist_isvisible(x) && glist_istoplevel(x))
         {
             sys_vgui("pdtk_undomenu .x%lx %s %s\n",
diff --git a/pd/src/x_preset.c b/pd/src/x_preset.c
index 31c5cd26c..8b2ef0ec9 100644
--- a/pd/src/x_preset.c
+++ b/pd/src/x_preset.c
@@ -656,7 +656,7 @@ void preset_node_clear(t_preset_node *x, t_float f)
             {
                 np1 = hd2->phd_npreset;
                 // if it is first one
-                if (np1->np_preset == (int)f)
+                if (np1 && np1->np_preset == (int)f)
                 {
                     hd2->phd_npreset = np1->np_next;
                     if (np1->np_val.l_n)
@@ -687,13 +687,12 @@ void preset_node_clear(t_preset_node *x, t_float f)
                 }
             }
         }
+        SETFLOAT(ap+0, f);
+        SETFLOAT(ap+1, (t_float)changed);
+        outlet_anything(x->pn_hub->ph_outlet, gensym("node_clear"), 2, ap);
     }
-    if (changed && !x->pn_hub->ph_extern_file)
+    if (changed && (!x->pn_hub || !x->pn_hub->ph_extern_file))
         canvas_dirty(x->pn_hub->ph_canvas, 1);
-
-    SETFLOAT(ap+0, f);
-    SETFLOAT(ap+1, (t_float)changed);
-    outlet_anything(x->pn_hub->ph_outlet, gensym("node_clear"), 2, ap);
 }
 
 
-- 
GitLab