Destructor bug in pgfortran

Dear PGI maintainers,

it seems that pgfortran compiled binaries fail to call destructors on intent(out) actual arguments. Given the example below, the finalizer is never called, although it should be as the actual argument is overwritten with a default initialized one.

module typedef
  implicit none

  type :: MyType
    integer :: status = 0
    final :: MyType_destruct
  end type MyType


  subroutine MyType_destruct(this)
    type(MyType), intent(inout) :: this

    print *, 'MyType_destruct: ', this%status
    this%status = -1

  end subroutine MyType_destruct

  subroutine createMyType(inst)
    type(MyType), intent(out) :: inst

    print *, 'createMyType: ', inst%status
    inst%status = 1

  end subroutine createMyType

end module typedef

program test
  use typedef
  implicit none

  type(MyType) :: inst

  inst%status = 2
  call createMyType(inst)
  print *, 'main program: ', inst%status

end program test

Using pgfortran 17.10, I obtain for the code above the output

 createMyType:             0
 main program:             1

while the expected one would be:

MyType_destruct:  2
 createMyType:  0
 main program:  1

Failing to call the destructor can lead to serious memory leakage, if the derived type encompasses a resource (e.g. an allocated pointer) much must be freed if the derived type instance goes out of scope.

Best regards,


Thanks for the example. We replicated your behavior and logged the issue as TPR 25165.