Community
cancel
Showing results for 
Search instead for 
Did you mean: 
pwl_b
Beginner
394 Views

A pointer passed to DEALLOCATE points to an object that cannot be deallocated

Compiling (with ifort 13.1.1) and executing the following code

[fortran]

module tt

  type :: t
  end type t

  type :: t1
     class(t), pointer :: tp
   contains
     procedure :: set
     procedure :: unset
  end type t1

contains

  function t_new() result(r)
    type(t), pointer :: r
    allocate(r)
  end function t_new

  subroutine set(a, tp)
    class(t1) :: a
    class(t), target :: tp
    a%tp => tp
  end subroutine set

  subroutine unset(a)
    class(t1) :: a
    print *, "Dealocating"
    deallocate(a%tp)
    print *, "Deallocated"
  end subroutine unset

end module tt


program test

  use tt

  type(t1) :: a

  call a%set(t_new())
  call a%unset()

end program test

[/fortran]

I get

[plain]

 Dealocating
forrtl: severe (173): A pointer passed to DEALLOCATE points to an object that cannot be deallocated
Image              PC                Routine            Line        Source             
pointer            000000000046D7AE  Unknown               Unknown  Unknown
pointer            000000000046C246  Unknown               Unknown  Unknown
pointer            0000000000424FC2  Unknown               Unknown  Unknown
pointer            00000000004068BB  Unknown               Unknown  Unknown
pointer            00000000004050E6  Unknown               Unknown  Unknown
pointer            0000000000402C57  Unknown               Unknown  Unknown
pointer            0000000000402B3C  Unknown               Unknown  Unknown
libc.so.6          00007FB324690A15  Unknown               Unknown  Unknown
pointer            0000000000402A39  Unknown               Unknown  Unknown

[/plain]

but using gfortran yields

[plain]

 Dealocating
 Deallocated

[/plain]

Is this a bug?

Best,

Paweł Biernat

0 Kudos
20 Replies
jimdempseyatthecove
Black Belt
16 Views

OOPS, I wrote the following before seeing the above post.

Ignore the following (as you are using TBP)

>> I'm having a similar difficulty of deallocating pointers to unlimited polymorphic components of a derived type.

The above states that you have an external (to the derived type) pointer to an allocatable within the derived type.

Should this component be ALLOCATABLE, it would also have to have the TARGET attribute .AND. the (external) pointer could not deallocate this component.

Should this component be POINTER (which is/was allocated), then the external pointer could perform the deallocate **** with your requirement to NULLIFY the component pointer, and all other pointers that may be pointing to that component.

You would be best served by creating a type bound function/subroutine to perform this.

Type-Bound Procedures (intel.com)

Jim Dempsey