From 725d40d5b74c572c17e428035598b18076d3855e Mon Sep 17 00:00:00 2001
From: Ivica Ico Bukvic <ico@vt.edu>
Date: Fri, 11 Nov 2016 20:34:31 -0500
Subject: [PATCH] *improved saving logic for presets to remove stale nodes
 because sometimes nodes due to editing can share locations with
 stale/inactive nodes and depending on the order of creation, invalid values
 can end-up being pulled. Now, when saving, only active nodes are saved to
 avoid this problem.

---
 pd/src/x_preset.c | 70 +++++++++++++++++++++++++----------------------
 1 file changed, 38 insertions(+), 32 deletions(-)

diff --git a/pd/src/x_preset.c b/pd/src/x_preset.c
index 8b2ef0ec9..d618696a8 100644
--- a/pd/src/x_preset.c
+++ b/pd/src/x_preset.c
@@ -973,39 +973,45 @@ void preset_hub_save(t_gobj *z, t_binbuf *b)
         phd = x->ph_data;
         while (phd)
         {
-            if(PH_DEBUG) fprintf(stderr,"    saving phd\n");
-            /* designate a node and state whether it is active or disabled
-               (disabled nodes are ones that have presets saved but have been
-               deleted since-- we keep these in the case of undo actions during
-               the session that may go beyond saving something into a file) */
-            binbuf_addv(b, "si", gensym("%node%"), phd->phd_pn_gl_loc_length);
-
-            // gather info about the length of the node's location and store it
-            for (i = 0; i < phd->phd_pn_gl_loc_length; i++)
-            {
-                binbuf_addv(b,"i", (int)phd->phd_pn_gl_loc[i]);
-            }
-
-            // save preset data
-            np = phd->phd_npreset;
-            while (np)
+        	// only save node and preset if it is active, this prevents stale
+        	// data that will be unnecessary since undo is also purged once
+        	// the file is reloaded from the disk
+        	if (phd->phd_node)
             {
-                if (np->np_val.l_n > 0)
-                {
-                    binbuf_addv(b, "si", gensym("%preset%"),
-                        (int)np->np_preset);
-                    for (i = 0; i < np->np_val.l_n; i++)
-                    {
-                        if (np->np_val.l_vec[i].l_a.a_type == A_FLOAT)
-                            binbuf_addv(b, "f",
-                                np->np_val.l_vec[i].l_a.a_w.w_float);
-                        else if (np->np_val.l_vec[i].l_a.a_type == A_SYMBOL)
-                            binbuf_addv(b, "s",
-                                np->np_val.l_vec[i].l_a.a_w.w_symbol);    
-                    }
-                }
-                np = np->np_next;
-            }
+	            if(PH_DEBUG) fprintf(stderr,"    saving phd\n");
+	            /* designate a node and state whether it is active or disabled
+	               (disabled nodes are ones that have presets saved but have been
+	               deleted since-- we keep these in the case of undo actions during
+	               the session that may go beyond saving something into a file) */
+	            binbuf_addv(b, "si", gensym("%node%"), phd->phd_pn_gl_loc_length);
+
+	            // gather info about the length of the node's location and store it
+	            for (i = 0; i < phd->phd_pn_gl_loc_length; i++)
+	            {
+	                binbuf_addv(b,"i", (int)phd->phd_pn_gl_loc[i]);
+	            }
+
+	            // save preset data
+	            np = phd->phd_npreset;
+	            while (np)
+	            {
+	                if (np->np_val.l_n > 0)
+	                {
+	                    binbuf_addv(b, "si", gensym("%preset%"),
+	                        (int)np->np_preset);
+	                    for (i = 0; i < np->np_val.l_n; i++)
+	                    {
+	                        if (np->np_val.l_vec[i].l_a.a_type == A_FLOAT)
+	                            binbuf_addv(b, "f",
+	                                np->np_val.l_vec[i].l_a.a_w.w_float);
+	                        else if (np->np_val.l_vec[i].l_a.a_type == A_SYMBOL)
+	                            binbuf_addv(b, "s",
+	                                np->np_val.l_vec[i].l_a.a_w.w_symbol);    
+	                    }
+	                }
+	                np = np->np_next;
+	            }
+	        }
 
             phd = phd->phd_next;
         }
-- 
GitLab