Nvfortran runtime error for nested types


the following program yields a runtime error when compiled with nvfortran 22.3.0:

module module_1
  implicit none

  public :: type_1
  public :: gen_type_1

  type :: type_1
    integer, dimension(:), allocatable :: numbers
  end type type_1

  elemental function gen_type_1(i) result (r)
     integer, intent(in) :: i
     type(type_1) :: r
     allocate (r%numbers(1))                                                                          
     r%numbers(1) = i                                                                                 
  end function gen_type_1                                                                             
end module module_1                                                                                   
module module_2                                                                                       
 use module_1 
 implicit none
 public :: type_2
 public :: foo                                                                                        
 type :: type_2                                                                                       
   type(type_1) :: value                                                                              
   procedure :: get_value                                                                             
 end type type_2
  elemental function get_value (self) result(v)                                                       
    class(type_2), intent(in) :: self                                                                 
    type(type_1) :: v
    v = self%value                                                                                    
  end function get_value    

  subroutine foo(arr_in)                                                                              
    type(type_2), dimension(:), intent(in) :: arr_in                                                  
    type(type_1), dimension(:), allocatable :: arr_cpy                                                
    integer :: i                                                                                      
    allocate (arr_cpy(size(arr_in)))                                                                  
    forall (i = 1:size(arr_in)) arr_cpy(i) = arr_in(i)%get_value()

  end subroutine foo                                                                                  
end module module_2 

program t
  use module_1
  use module_2
  implicit none

  type(type_2), dimension(:), allocatable :: arr
  integer, parameter :: n = 1000
  integer :: i
  allocate (arr(n))
  do i = 1, n
     arr(i)%value = gen_type_1(0)
  end do
  call foo(arr)
end program t

The error message is

*** Error in `./test.x': double free or corruption (fasttop): 0x000000000180c6f0 ***

The call to the foo function at the very end is crucial. Without it, there is no error. I guess that all this indicates that when the nested types are freed up at the end of the function, the deallocation makes mistakes with the different levels of this type.

Best regards,

Thanks Cristian,

My best guess is that the compiler is doing some type of implicit allocation/deallocation and freeing something twice. I added a problem report, TPR#32307, and sent it to engineering for further investigation.


1 Like