Open carlosggarcia opened 7 years ago
The above solution is not completely satisfactory since it leads to memory leakages. In particular, pba->parameters_smg
(see https://github.com/miguelzuma/hi_class_public/blob/hi_class/source/background.c#L675) is still allocated. I suppose it is the same for pba->scf_parameters
and the others.
The following may be correct but I haven't tested it with ncdm species nor scf.
python/cclassy.pxd
@@ -57,6 +57,7 @@ cdef extern from "class.h":
double w0_fld
double wa_fld
double cs2_fld
+ short shooting_failed
int bt_size
@@ -249,6 +250,7 @@ cdef extern from "class.h":
void perturb_free(void*)
void thermodynamics_free(void*)
void background_free(void*)
+ void background_free_input(void*)
void nonlinear_free(void*)
cdef int _FAILURE_
python/classy.pyx
@@ -323,6 +323,9 @@ cdef class Class:
#NOTE: Added ready=true and add(perturb) at the steps to avoid memory leaks if code fails (MZ)
if "background" in level:
if background_init(&(self.pr), &(self.ba)) == _FAILURE_:
+ if self.ba.shooting_failed:
+ background_free_input(&self.ba)
+ raise CosmoComputationError(self.ba.error_message)
self.ncp.add("background")
self.ready = True
self.struct_cleanup()
I hope it helps.
Hi,
Working in hi_class with models where shooting fails quite often I encountered segmentation fault. I reproduce below the relevant part of the output error. This error was raised only (i don't really understand why) when, after a successful computation and posterior struct_cleanup, I computed with a different set of parameters for which shooting fails .
In some of my trials and errors I also experienced these other two segmentation faults (they were far less common, though):
I traced back the origin of the error and saw that the segmentation fault was caused in background_free() when calling:
I don't understand why
free(pba->d2tau_dz2_table)
caused no problem.The point is that after a failed shooting, background_init() exits without initializing them. In fact, if in main/class.c we do:
valgrind reports
These errors confirmed my suspicions. In the present state of classy.pyx we have:
so that every time background fails, no matter why, background structures are freed. In my case, since background structure was uninitialized due to a failed shooting, it caused segmentation fault. The solution I implemented and seems to work is checking whether the failure is caused by a failed shooting or not :
I don't know if I should have made a pull request instead of issuing this. In case you think so, I will as soon as possible.
Thanks.