From 2ab9809aee45738f2ba3c39e2a56bde7e2499e45 Mon Sep 17 00:00:00 2001
From: Robert Izzard <r.izzard@surrey.ac.uk>
Date: Sun, 11 Jul 2021 13:18:43 +0100
Subject: [PATCH] attempt to clean up unfreed memory

---
 src/events/catch_events.c              |  7 +++++++
 src/evolution/evolution_rejected.c     | 10 ++++++++++
 src/evolution/evolution_split.c        | 10 ++++++++++
 src/evolution/evolve_system_binary_c.c |  1 -
 src/memory/deep_copy_stardata.c        |  5 +++++
 src/memory/free_stardata.c             |  4 ++++
 6 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/src/events/catch_events.c b/src/events/catch_events.c
index 8b20705b0..b2dac3698 100644
--- a/src/events/catch_events.c
+++ b/src/events/catch_events.c
@@ -34,11 +34,18 @@ void catch_events(struct stardata_t * RESTRICT const stardata)
          */
         if(stardata->preferences->save_pre_events_stardata == TRUE)
         {
+            if(stardata->pre_events_stardata != NULL)
+            {
+                /* this should not happen! */
+                free_stardata(&stardata->pre_events_stardata);
+            }
             stardata->pre_events_stardata = new_stardata(stardata->preferences);
             copy_stardata(stardata,
                           stardata->pre_events_stardata,
                           COPY_STARDATA_PREVIOUS_NONE,
                           COPY_STARDATA_PERSISTENT_NONE);
+            /* prevent recursion */
+            stardata->pre_events_stardata->pre_events_stardata = NULL;
         }
 
         if(stardata->preferences->catch_events_function != NULL)
diff --git a/src/evolution/evolution_rejected.c b/src/evolution/evolution_rejected.c
index 9945db89c..b01ce8360 100644
--- a/src/evolution/evolution_rejected.c
+++ b/src/evolution/evolution_rejected.c
@@ -42,6 +42,16 @@ int evolution_rejected(struct stardata_t * RESTRICT const stardata)
            stardata->previous_stardata->model.supernova
         );
 
+    /*
+     * Free any pre-event data
+     */
+    if(stardata->pre_events_stardata != NULL)
+    {
+        printf("reject free pre %p\n",
+               (void*)stardata->pre_events_stardata);
+        free_stardata(&stardata->pre_events_stardata);
+    }
+
     /*
      * Restore the previous stardata and reduce the timestep
      * if reject == REJECTION_RESULT_SHORTEN_TIMESTEP
diff --git a/src/evolution/evolution_split.c b/src/evolution/evolution_split.c
index 0c934cfd8..ba894bf94 100644
--- a/src/evolution/evolution_split.c
+++ b/src/evolution/evolution_split.c
@@ -147,6 +147,7 @@ int evolution_split(struct stardata_t * RESTRICT const stardata,
 
                     erase_events(stardata);
                     free_previous_stardatas(stardata);
+                    free_stardata(&stardata->pre_events_stardata);
 
                     copy_stardata(p->stardata,
                                   stardata,
@@ -186,6 +187,7 @@ int evolution_split(struct stardata_t * RESTRICT const stardata,
                     /* clean up memory */
                     struct stardata_t * s = stardata->preferences->splitinfo[stardata->preferences->current_splitdepth]->stardata;
 
+                    free_stardata(&stardata->pre_events_stardata);
                     free_previous_stardatas(s);
                     Safe_free(s);
                     Safe_free(stardata->preferences->splitinfo[stardata->preferences->current_splitdepth]);
@@ -225,6 +227,12 @@ int evolution_split(struct stardata_t * RESTRICT const stardata,
                 /* should be rerun this many times */
                 p->count = split - 1;
 
+                /*
+                 * Free the pre-events stardata, as this event
+                 * will be rerun
+                 */
+                free_stardata(&stardata->pre_events_stardata);
+
                 /*
                  * allocate space
                  */
@@ -240,6 +248,8 @@ int evolution_split(struct stardata_t * RESTRICT const stardata,
                     COPY_STARDATA_PERSISTENT_FROM_POINTER
                     );
 
+
+
                 /*
                  * Keep preferences
                  */
diff --git a/src/evolution/evolve_system_binary_c.c b/src/evolution/evolve_system_binary_c.c
index 0e9891744..143fd4c2e 100644
--- a/src/evolution/evolve_system_binary_c.c
+++ b/src/evolution/evolve_system_binary_c.c
@@ -229,7 +229,6 @@ int evolve_system_binary_c(struct stardata_t * RESTRICT const stardata)
             /*
              * This step has been rejected.
              */
-
             if(reject == EVOLUTION_REJECT_AND_RETURN_NOW)
             {
                 /*
diff --git a/src/memory/deep_copy_stardata.c b/src/memory/deep_copy_stardata.c
index 53847ce53..246262766 100644
--- a/src/memory/deep_copy_stardata.c
+++ b/src/memory/deep_copy_stardata.c
@@ -52,6 +52,11 @@ void * deep_copy_stardata(struct stardata_t * const src,
      */
     memcpy(dest,src,sizeof(struct stardata_t));
 
+    /*
+     * Don't keep pre_events_stardata
+     */
+    dest->pre_events_stardata = NULL;
+
 #ifdef MINT
     Cprint("post-memcpy old mint %p, new mint %p (should be same)\n",
            (void*)src->star[0].mint,
diff --git a/src/memory/free_stardata.c b/src/memory/free_stardata.c
index 16eb74a3a..66475ec44 100644
--- a/src/memory/free_stardata.c
+++ b/src/memory/free_stardata.c
@@ -15,6 +15,10 @@ void free_stardata(struct stardata_t ** sp)
         {
             free_star_contents(&stardata->star[k]);
         }
+        if(stardata->pre_events_stardata != NULL)
+        {
+            free_stardata(&stardata->pre_events_stardata);
+        }
 #ifdef ORBITING_OBJECTS
         if(stardata->common.orbiting_object != NULL)
         {
-- 
GitLab