[Mesa-users] Fixed many memory leaks

Michael Ashley m.ashley at unsw.edu.au
Tue Apr 10 17:58:25 EDT 2018


Dear all,

Just a comment that fixing leaks such as this is one good use of a "goto" statement. E.g., it is standard practice in the Linux kernel software to use gotos to unwind things like memory allocation, file opening, etc. It leads to simpler code that it easier to inspect for errors. The alternative is a maze of if/else statements that are harder to get right.

Cheers, Michael

On Tue, Apr 10, 2018 at 07:51:00PM +0000, Zhang, Zhaoxi M. (Michael) wrote:
> Hello everyone,
> 
> 
> I'm running into a problem where if I leave a model running for too long with
> diffusion enabled, MESA takes up more and more memory, eventually getting
> killed by the operating system because the system runs out of memory.  I used
> valgrind to check for memory leaks, and found the cause of these problems.  A
> patch is attached. 
> 
> 
> There are many areas of the code that look like this:
> 
> 
> call do_alloc
> 
> 
> call do_step_1(..., ..., ierr)
> 
> if (ierr /= 0) return
> 
> 
> call do_step_2(..., ..., ierr)
> 
> if (ierr /= 0) return
> 
> 
> call dealloc
> 
> 
> If any of the steps between do_alloc and dealloc encounter an error, the
> subroutine returns without freeing the allocated memory.  In my case, "fixup"
> often fails in diffusion, leading to a constant stream of leaks.
> 
> 
> The patch doesn't fix all the potential memory leaks.  If do_alloc ever fails,
> the function returns without calling dealloc.  If do_alloc succeeds in
> allocating some but not all of the arrays, it might be dangerous to call
> dealloc because it might try to free unallocated arrays. 
> 
> 
> valgrind also detected one other definite memory leak and 2 probable ones, but
> they seem to be small one-time leaks relating to MESA's startup routines.    
> The definite memory leak is probably because Free_Kap_CO_Tables is never
> called, but I don't know for sure.  I've attached valgrind's output in case
> someone wants to try finding the leak.
> 
> 
> 
> Cheers,
> 
> 
> Michael
> 
> 
> 
> 



> _______________________________________________
> mesa-users at lists.mesastar.org
> https://lists.mesastar.org/mailman/listinfo/mesa-users
> 


-- 
Professor Michael Ashley                   Department of Astrophysics
University of New South Wales       http://www.phys.unsw.edu.au/~mcba



More information about the Mesa-users mailing list