Crash if temporary implicit pointer is created

Crash if temporary implicit pointer is created


Could you please explain the crash in the following program?

module A_m

	  implicit none



	  type, public :: A_t


	    character(len=1)  :: dummy_char


	    procedure :: DoSmth

	  end type

	  subroutine DoSmth(this)

	    class(A_t), intent(in) :: this

	    write(*, "(A)") "A_t::DoSmth"

	  end subroutine DoSmth
end module A_m

	module B_m

	  use A_m

	  implicit none

  type, public :: B_t


	    class(A_t), pointer :: a => null()


	    procedure :: GetA

	    procedure :: SetA

	  end type

	  subroutine SetA(this, a)

	    class(B_t), intent(inout)         :: this

	    class(A_t), pointer, intent(in)   :: a

	    this%a => a

	  end subroutine SetA
  function GetA(this) result(a)

	    class(B_t), target, intent(in) :: this

	    class(A_t), pointer            :: a

	    a => this%a

	  end function GetA
end module B_m

	program main

	  use A_m

	  use B_m

	  implicit none

	  class(A_t), pointer :: a

	  class(B_t), pointer :: b

	  class(A_t), pointer :: a_ptr
  allocate(a, b)

	  call b%SetA(a)
  ! works with explicit pointer

	  a_ptr => b%GetA()

	  call DoA(a_ptr)
  ! crashes here

	  call DoA(b%GetA())
  deallocate(a, b)

	  subroutine DoA(a)

	    class(A_t), pointer, intent(in) :: a

	    if (.not.associated(a)) then

	      write(*, "(A)") "error"


	      call a%DoSmth()

	    end if

	  end subroutine DoA
end program main

I believe that the program crashes because an implicit pointer is created at the statement "call DoA(b%GetA())".

Compiler version: XE 13.1

2 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

I am looking at this now.

Retired 12/31/2016

Leave a Comment

Please sign in to add a comment. Not a member? Join today