memory leak in libgfortran
Daniel Franke
franke.daniel@gmail.com
Wed Sep 13 20:17:00 GMT 2006
Hi all,
while checking for memory leaks in my own program, I found one
in libgfortran (so I think). Please find a valgrind session log
and the stripped down code below.
The number of bytes lost depends on the initial length of:
45 CHARACTER(len=256) :: input
The validation function and the SELECT CASE at line 29 are essential,
if either is replaced, no memory is leaked.
I can reproduce the leak using gfortran-4.1.1 and gfortran-4.2 (20060906).
Shall I open a PR for this?
Regards
Daniel
$> valgrind --tool=memcheck --leak-check=full ./a.out
==19151== Memcheck, a memory error detector.
==19151== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
==19151== Using LibVEX rev 1606, a library for dynamic binary translation.
==19151== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
==19151== Using valgrind-3.2.0, a dynamic binary instrumentation framework.
==19151== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
==19151== For more details, rerun with: -v
==19151==
[a]ngstrom, [n]anometer: x
[a]ngstrom, [n]anometer: x
[a]ngstrom, [n]anometer: x
[a]ngstrom, [n]anometer: x
[a]ngstrom, [n]anometer: a
==19151==
==19151== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 3 from 1)
==19151== malloc/free: in use at exit: 1,280 bytes in 5 blocks.
==19151== malloc/free: 21 allocs, 16 frees, 50,128 bytes allocated.
==19151== For counts of detected errors, rerun with: -v
==19151== searching for pointers to 5 not-freed blocks.
==19151== checked 77,788 bytes.
==19151==
==19151== 1,280 bytes in 5 blocks are definitely lost in loss record 1 of 1
==19151== at 0x4021396: malloc (vg_replace_malloc.c:149)
==19151== by 0x40304FC: _gfortrani_get_mem (memory.c:53)
==19151== by 0x80488FA: __configuration__validate_value (dump.f90:29)
==19151== by 0x8048AA5: MAIN__ (dump.f90:50)
==19151== by 0x8048AE6: main (fmain.c:18)
==19151==
==19151== LEAK SUMMARY:
==19151== definitely lost: 1,280 bytes in 5 blocks.
==19151== possibly lost: 0 bytes in 0 blocks.
==19151== still reachable: 0 bytes in 0 blocks.
==19151== suppressed: 0 bytes in 0 blocks.
==19151== Reachable blocks (those to which a pointer was found) are not shown.
==19151== To see them, rerun with: --show-reachable=yes
$> cat leak.f90
MODULE stringutils
CHARACTER(*), PRIVATE, PARAMETER :: lowercase = 'abcdefghijklmnopqrstuvwxyz'
CHARACTER(*), PRIVATE, PARAMETER :: uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
CONTAINS
FUNCTION tolower(instr) RESULT (outstr)
CHARACTER(len=*), INTENT(in) :: instr
CHARACTER(len=len(instr)) :: outstr
INTEGER :: i, k
outstr = instr
DO i = 1, len_trim(outstr)
k = index(uppercase, outstr(i:i))
if (k /= 0) outstr(i:i) = lowercase(k:k)
END DO
END FUNCTION
END MODULE
MODULE configuration
CONTAINS
LOGICAL FUNCTION validate_value(input, value)
USE stringutils
CHARACTER(len=*), INTENT(in) :: input
INTEGER, INTENT(out) :: value
validate_value = .TRUE.
SELECT CASE (tolower(input))
CASE ("a", "angstrom"); value = 1
CASE ("n", "nanometer"); value = 2
CASE DEFAULT
validate_value = .FALSE.
END SELECT
END FUNCTION
END MODULE
PROGRAM test
USE configuration
USE stringutils
IMPLICIT NONE
INTEGER :: value
CHARACTER(len=256) :: input
DO
WRITE (*, FMT="(A)", ADVANCE = "NO") "[a]ngstrom, [n]anometer: "
READ (*, FMT="(A)") input
IF (validate_value(input, value)) EXIT
END DO
END PROGRAM
More information about the Fortran
mailing list