Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Pointer release problem

Li_Dong
Beginner
986 Views
Dear all,
Here is a simplified case:
[fortran]program main

    type A
    end type A

    type, extends(A) :: B
        integer :: i = 0
    end type B

    type(B), pointer :: p1
    class(A), pointer :: p2

    allocate(p1)
    p2 => p1
    deallocate(p2)

end program main[/fortran]
I would like to release the memory allocated by "p1" through the handle "p2", but this case doesn't work.
How to accomplish it? Thanks!
dongli
0 Kudos
7 Replies
mecej4
Honored Contributor III
986 Views
This is a dangerous way of handling pointers: allocating through p1, associating p2 with p1 and then deallocating through p2 leaves p1 associated but dangling, i.e., pointing to freed memory and, therefore, unusable.

Try adding

if(associated(p1))write(*,*) ' p1 is still associated'

after line 15 and see what is printed.
0 Kudos
Li_Dong
Beginner
986 Views
Dear mecej4,
Actually, line 15 is not executed successfully at run time.
The error message is:
forrtl: severe (173): A pointer passed to DEALLOCATE points to an array that cannot be deallocated
dongli
0 Kudos
mecej4
Honored Contributor III
986 Views
The action taken (if any) seems to depend on the compiler used. With this modification of your code:

[fortran]program main

type A
end type A

type, extends(A) :: B
integer :: i = 0
end type B

type(B), pointer :: p1
class(A), pointer :: p2
integer :: istat

allocate(p1,stat=istat)
if(istat.ne.0)write(*,*)' Istat 1 : ',istat
p2 => p1
deallocate(p2,stat=istat)
if(istat.ne.0)write(*,*)' Istat 2 : ',istat
if(associated(p1))write(*,*) ' p1 is still associated'

end program main
[/fortran]
Intel Fortran 12.0 says

[bash]  Istat 2 :          173
p1 is still allocated
[/bash]
whereas Gfortran 4.5 prints only the second line. NAG Fortran 5.2 says:
[bash]Runtime Error: ptr.f90, line 19: Dangling pointer P1 used as argument to intrinsic function ASSOCIATED
Target was DEALLOCATEd at line 17 of ptr.f90
Program terminated by fatal error
ptr.f90, line 19: Error occurred in MAIN[/bash]

0 Kudos
Li_Dong
Beginner
986 Views
From your results, it seems that only NAG Fortran 5.2 gives the expected result!
0 Kudos
Steven_L_Intel1
Employee
986 Views
All of the compilers give the result expected by the standard, though I like NAG's the best. If you deallocate all or part of storage pointed to by a pointer, that pointer's association status becomes undefined and calling ASSOCIATED on it or using the pointer is not allowed by the standard - results are unpredictable.
0 Kudos
Li_Dong
Beginner
986 Views
I think the "associated" intrinsic function should provide more information, such as the content of pointer p1 has been released by another pointer p2, then associated(p1) should tell me ".false.".
0 Kudos
jimdempseyatthecove
Honored Contributor III
986 Views
Associated cannot tell you that. It is your responsibility to dissassociate all pointers to object being deleted. Should that memory get re-allocated for something else, or a different object of the same type then a "pointing to allocated memory" indicator will produce a false positive (.false. for pointing to what you think it was).

Jim Dempsey
0 Kudos
Reply