https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61831

--- Comment #43 from Dominique d'Humieres <dominiq at lps dot ens.fr> ---
> The testcase passes with it, at the price of leaking memory

Yes: gfortran.dg/alloc_comp_constructor_1.f90 (17 builtin_free instead of 19)
and gfortran.dg/class_array_15.f03 (11 builtin_free instead of 12). I think to
fix this PR without memory leak one has to avoid to free the temporary for
elemental procs

For the reduced test

program main
  implicit none

  integer, parameter :: n = 2

  type :: string_t
     character(LEN=1), dimension(:), allocatable :: chars
  end type string_t

  type :: string_container_t
     type(string_t) :: comp
  end type string_container_t

  type :: string_array_container_t
     type(string_t) :: comp(n)
  end type string_array_container_t

  type(string_t) :: prt_in, tmp, tmpa(n)
  type(string_container_t) :: tmpc, tmpca(n)
  type(string_array_container_t) :: tmpac, tmpaca(n)
  integer :: i, j, k

  do i=1,16

     ! Test without intermediary function
     prt_in = string_t(["A"])
     if (.not. allocated(prt_in%chars)) call abort
     if (any(prt_in%chars .ne. "A")) call abort
     deallocate (prt_in%chars)

     ! scalar elemental function with structure constructor
     prt_in = string_t(["D"])
     if (.not. allocated(prt_in%chars)) call abort
     if (any(prt_in%chars .ne. "D")) call abort
     tmpc = new_prt_spec2 (string_container_t(prt_in))
     if (.not. allocated(prt_in%chars)) call abort
     if (any(prt_in%chars .ne. "D")) call abort
     deallocate (prt_in%chars)
     deallocate(tmpc%comp%chars)

  end do

contains

  elemental function new_prt_spec2 (name) result (prt_spec)
    type(string_container_t), intent(in) :: name
    type(string_container_t) :: prt_spec
    prt_spec = name
  end function new_prt_spec2

end program main

I also see at run time (with/without the patch in comment 42)

a.out(69499,0x7fff748b3300) malloc: *** mach_vm_map(size=18446603339087888384)
failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x10a9e4c62
#1  0x10a9e3f80
#2  0x7fff8de21f19
Segmentation fault

Reply via email to