From 1aaa0b52009630bfe98a3255884ed1f6f8dbb7c5 Mon Sep 17 00:00:00 2001
From: Robert Izzard <r.izzard@surrey.ac.uk>
Date: Mon, 16 Sep 2019 18:44:15 +0100
Subject: [PATCH] clean up of memory leak in discs code

---
 src/binary_c_parameters.h                           | 8 +++++---
 src/common_envelope/common_envelope_evolution_BSE.c | 2 ++
 src/disc/disc_initialize_disc.c                     | 2 ++
 src/disc/disc_macros.h                              | 1 -
 src/disc/remove_disc.c                              | 9 +++++++--
 tbse                                                | 4 ++--
 test_random.pl                                      | 2 +-
 7 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/src/binary_c_parameters.h b/src/binary_c_parameters.h
index e1a4368e8..b382a3df0 100644
--- a/src/binary_c_parameters.h
+++ b/src/binary_c_parameters.h
@@ -1394,9 +1394,7 @@
  * see nucsyn/nucsyn.h for details of parts you can switch on 
  * off.
  ************************************************************/
-#ifndef BINT
-//#define NUCSYN
-#endif
+#define NUCSYN
 
 /************************************************************
  * Stellar colour library
@@ -1936,6 +1934,10 @@
 #Error: you should not define BOTH BSE and BINT
 #endif
 
+#if defined BINT && defined NUCSYN
+#Error: you should not define BOTH BINT and NUCSYN
+#endif
+
 /*
  * Use new version of difflog that requires fewer memcpys 
  */
diff --git a/src/common_envelope/common_envelope_evolution_BSE.c b/src/common_envelope/common_envelope_evolution_BSE.c
index f41fdda7a..0333ab15f 100644
--- a/src/common_envelope/common_envelope_evolution_BSE.c
+++ b/src/common_envelope/common_envelope_evolution_BSE.c
@@ -2122,7 +2122,9 @@ end_section:
                 if(cbdisc->memo)
                 {
                     Dprint("call memoize_free on appended cbdisc\n");
+                    printf("comenv double disc memoize free %p\n",cbdisc->memo); fflush(NULL);
                     memoize_free(&cbdisc->memo);
+                    printf("comenv double disc now %p\n",cbdisc->memo);fflush(NULL);
                 }
                 Set_logstring(LOG_DISC,
                               "0Append CB disc: age=%g y, M+=%g=%g, Msun, J+=%g=%g (+=%g and +=%g %% of CE) ",
diff --git a/src/disc/disc_initialize_disc.c b/src/disc/disc_initialize_disc.c
index ffa918939..b8db91fde 100644
--- a/src/disc/disc_initialize_disc.c
+++ b/src/disc/disc_initialize_disc.c
@@ -70,8 +70,10 @@ void disc_initialize_disc(struct stardata_t * const stardata,
     disc->prevzone = DISC_NO_PREVZONE_YET;
 #endif
 #ifdef MEMOIZE
+    printf("disc memoize init was %p\n",disc->memo);
     disc->memo = NULL;
     memoize_initialize(&disc->memo);
+    printf("disc memoize init %p\n",disc->memo);
 #endif
 
     /*
diff --git a/src/disc/disc_macros.h b/src/disc/disc_macros.h
index 628948d2a..d9052ce9e 100644
--- a/src/disc/disc_macros.h
+++ b/src/disc/disc_macros.h
@@ -306,7 +306,6 @@
     (D)->J = 0.0;                                                       \
     (D)->dt = LONG_TIMESTEP;                                            \
     (D)->converged = FALSE;                                             \
-    if((D)->memo)memoize_free(&(D)->memo);                              \
     if(DISC_DEBUG)                                                      \
         printf("Disc evaporated at line %d in file %s because %s\n",    \
                __LINE__,                                                \
diff --git a/src/disc/remove_disc.c b/src/disc/remove_disc.c
index a8dad79a1..b64bf6601 100644
--- a/src/disc/remove_disc.c
+++ b/src/disc/remove_disc.c
@@ -31,7 +31,9 @@ void remove_disc(
         }
         int n = DISC_POS_TO_N(star,pos);
 #ifdef MEMOIZE
+        printf("remove_disc memoize_free %p\n",star->discs[n]->memo);fflush(NULL);
         memoize_free(&star->discs[n]->memo);
+        printf("remove_disc now %p\n",star->discs[n]->memo);fflush(NULL);
 #endif // MEMOIZE
         Safe_free(star->discs[n]);
         int i;
@@ -98,15 +100,18 @@ void remove_disc(
                 struct disc_t * disc = &common->discs[n];
 #ifdef MEMOIZE
                 if(stardata->previous_stardata->common.discs[n].memo ==
-                   stardata->common.discs[n].memo)
+                   stardata->common.discs[n].memo &&
+                    stardata != stardata->previous_stardata)
                 {
                     /* the memo will be freed, so remove the reference to it */
                     stardata->previous_stardata->common.discs[n].memo = NULL;
                 }
-                if(disc->memo)
+                if(disc->memo != NULL)
                 {
                     Dprint("call memoize_free\n");
+                    printf("remove_disc memoize_free %p\n",disc->memo);fflush(NULL);
                     memoize_free(&disc->memo);
+                    printf("remove_disc now %p\n",disc->memo);fflush(NULL);
                 }
 #endif//MEMOIZE
                 memset(disc,0,sizeof(struct disc_t));
diff --git a/tbse b/tbse
index 733ce1c1a..ffbe5df6e 100755
--- a/tbse
+++ b/tbse
@@ -572,10 +572,10 @@ DISC_TIMESTEP_FACTOR=1.0
 # DISC_LOG_LEVEL_SUBTIMESTEP 2 = on with sub-timestep logging
 # DISC_LOG_LEVEL_NORMAL_FIRST_DISC_ONLY = as 1 for first disc only
 # DISC_LOG_LEVEL_SUBTIMESTEP_FIRST_DISC_ONLY = as 2 for first disc only
-DISC_LOG=DISC_LOG_LEVEL_SUBTIMESTEP_FIRST_DISC_ONLY
+DISC_LOG=DISC_LOG_LEVEL_NONE
 
 # soft 2D disc log switch, same options as DISC_LOG
-DISC_LOG2D=DISC_LOG_LEVEL_SUBTIMESTEP_FIRST_DISC_ONLY
+DISC_LOG2D=DISC_LOG_LEVEL_NONE
 
 # disc log every DISC_LOG_DT Myr only. Ignored if zero.
 # If set to -1, is automatically calculated (1Myr except on the GB/AGB)
diff --git a/test_random.pl b/test_random.pl
index a501631ea..44802000a 100755
--- a/test_random.pl
+++ b/test_random.pl
@@ -27,7 +27,7 @@ check_ps() unless($args=~/skip_valgrind_check/);
 $SIG{INT} = \&reaper;
 my $valgrind = ($args=~/valgrind/) ? 'valgrind' : ''; # valgrind or ''
 my $nice = ($args=~/nice=(\d+)/) ? "nice -n +$1" : 'nice';
-my $timeout = ($args=~/timeout=(\d+)/) ? "timeout $1" : '';
+my $timeout = ($args=~/timeout=(\d+)/) ? "timeout -s 9 $1" : '';
 my $repeat = ($args=~/repeat=(\d+)/)[0] // 1;
 my $logtimes = ($args=~/logtimes/) ? 1 : 0;
 my $ignore_time = ($args=~/ignore_time=(\S+)/)[0] // 2.0; # do not log time if < this
-- 
GitLab