Open miguelzuma opened 9 years ago
Hi @miguelzuma,
thanks for noticing the problem and reporting it! I think there should be an even better way of fixing this issue - I will try to look into it in the coming week. The problem seems general enough in case of failure - so the fix should be there for everyone. I'll post updates on the issue.
I'm experiencing problems with memory leaks for models in which a class_test is triggered during the execution (this is in a modified version of class). After talking to Julien I used class_test_except to make sure I correctly freed the intermediate structures, but the problem persists.
Finally, I tracked the problem down to the classy wrapper, which fails to free the corresponding structure (e.g. call 'background_free'). This happens because in the function 'compute' each module is called in the following way:
if "background" in level: if background_init(&(self.pr), &(self.ba)) == FAILURE: self.struct_cleanup() raise CosmoComputationError(self.ba.error_message) self.ncp.add("background")
but in struct_cleanup, the procedure is
if self.ready == FALSE: return (...) if "background" in self.ncp: background_free(&self.ba)
When an error occurs (e.g. in background_init), struct_cleanup does not free the relevant structures for two reasons: 1) self.ready is FALSE, so the function terminates and 2) self.ncp does not contain "background" because this is added only if background_init is successful.
I've sort of made my way around it by correcting both problems whenever an structure_init returns FAILURE. This was simply by adding
self.ncp.add("background") self.ready = TRUE
before self.struct_cleanup() in 'compute', and similarly for other structures (thermodynamics, perturb, etc...). This seems to fix a lot of the problems that I was having when using my modified code in MontePython.
I'm submitting this as an issue rather than a pull request because 1) I think other people don't have this problem and 2) this is my first contact with classy and I might be easily doing something wrong.