Memory leaks in standard code
Janne Blomqvist
blomqvist.janne@gmail.com
Fri Feb 15 14:28:00 GMT 2008
FX wrote:
> Hi all,
>
> I just have dusted a patch I had that instrumentalizes all memory
> allocation and deallocation routines, allowing us to do whatever we
> want to track them in the library. A few uses that pop to mind are
> memory leaks and memory allocation statistics. I realize, though, that
> having memory leaks is rather hard in Fortran, because of the
> automatic deallocation on procedure exit... so I'm asking a very naive
> question: is it possible at all? Would a -fcheck=memleak option be
> useful at all? (With my current patch, all it requires it 20 more
> lines of code in the library, so it's a rather low-hanging fruit.)
With allocatables, it's not possible to leak memory (barring a
compiler/runtime bug of course). As you say, the standard requires the
space to be automatically deallocated when the variable goes out of scope.
But with pointers, the programmer is responsible for deallocation, just
like in C. That being said, pointers also allow aliasing, so a pointer
going out of scope without the target being deallocated is not proof of
a memory leak (since there could be another pointer variable pointing to
the same storage). A think it might be somewhat nontrivial to figure out
leaks due to this.
But I have to confess I'm a bit ignorant of what this patch is supposed
to do, compared to say using valgrind or similar tools.
--
Janne Blomqvist
More information about the Fortran
mailing list