Curious Memory Loss using a type binded function with list arguments in Fortran

Viewed 42

considering the following type defined in the module string_mod:

module string_mod
    implicit none
    private
    public ::string
    type string
        private
        Character(len=:), allocatable :: fchar

        contains
            procedure, public ::  arr_fn => string_arr_fn
            final :: clear

    end type

    interface string
        module procedure :: string_from_fchar
    end interface

    contains
        function string_from_fchar(fchr) result(str)
            Implicit None
            Character(len=*), intent(in) ::fchr
            Type(String) :: str

            allocate(str%fchar, source = fchr)
        end function string_from_fchar

        function string_arr_fn(this,other) result(res_str)
            Implicit None

            Class(String), intent(in) :: this, other(:)
            Type(String) :: res_str
            Integer :: jj
        end function

        subroutine clear(this)
            Implicit None
            Type(String), intent(inout) :: this
            if(allocated(this%fchar)) deallocate(this%fchar)
        end subroutine
end module

which is a simple wrapper saving a allocatable character of variable length. The type provides an Constructor from character, a destructor and a type bounded function arr_fn => string_arr_fn which excepts a list of String types and (in this example) is simply empty.

Now using this module in the following toy program:

program test

    use string_mod

    call create_tstr()

    contains

        subroutine create_tstr()
            Implicit none

            Type(String) :: tstr

            tstr = tstr%arr_fn([String("Hallo"),String("World")])

        end subroutine
end program test

compiling it with gefortran 9.3.0 and run it with valgrind, I find the following output:

==7319== Memcheck, a memory error detector
==7319== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==7319== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==7319== Command: ./a.out
==7319== 
==7319== 
==7319== HEAP SUMMARY:
==7319==     in use at exit: 10 bytes in 2 blocks
==7319==   total heap usage: 27 allocs, 25 frees, 13,613 bytes allocated
==7319== 
==7319== 5 bytes in 1 blocks are definitely lost in loss record 1 of 2
==7319==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==7319==    by 0x10971C: __string_mod_MOD_string_from_fchar (string_test.f90:25)
==7319==    by 0x10987F: create_tstr.4008 (string_test.f90:58)
==7319==    by 0x109C5B: MAIN__ (string_test.f90:47)
==7319==    by 0x109C93: main (string_test.f90:45)
==7319== 
==7319== 5 bytes in 1 blocks are definitely lost in loss record 2 of 2
==7319==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==7319==    by 0x10971C: __string_mod_MOD_string_from_fchar (string_test.f90:25)
==7319==    by 0x10989E: create_tstr.4008 (string_test.f90:58)
==7319==    by 0x109C5B: MAIN__ (string_test.f90:47)
==7319==    by 0x109C93: main (string_test.f90:45)
==7319== 
==7319== LEAK SUMMARY:
==7319==    definitely lost: 10 bytes in 2 blocks
==7319==    indirectly lost: 0 bytes in 0 blocks
==7319==      possibly lost: 0 bytes in 0 blocks
==7319==    still reachable: 0 bytes in 0 blocks
==7319==         suppressed: 0 bytes in 0 blocks
==7319== 
==7319== For lists of detected and suppressed errors, rerun with: -s
==7319== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

which seems to indicate some memory loss during the arr_fn call. The same code compiled with the ifort 19.0.5.281 is memory loss free by the way. Is there a mistake with my code or is this a bug in the gfortran compiler?

Best regards and thanks for help in advance.

0 Answers
Related