I have following Fortran code
type t_octree
integer :: max_num_point
class(t_octree_node), pointer :: root => null()
contains
final :: DESTROY_OCTREE
end type t_octree
type t_octree_node
real :: box(2,3)
integer :: depth
type(t_octree_node), pointer :: parent => null()
type(t_octree_node), pointer :: children(:) => null()
class(t_octree_leaf), pointer :: leaf => null()
contains
final :: CLEAN_NODE
end type t_octree_node
type, extends(t_octree_node) :: t_octree_leaf
integer, allocatable :: id(:)
integer :: num_point
contains
final :: DESTROY_LEAF
end type
After I have done my processing and now need to make sure that my stuff is deallocated properly.
In my `final routines I have
subroutine DESTROY_OCTREE( this )
implicit none
type(t_octree) :: this
type(t_octree_node), pointer :: node => null()
integer :: i
node => this% root
if( associated(node% leaf) )deallocate( node% leaf )
if(.not. associated(node% children) )RETURN
deallocate( node )
node => this% root
i = 0
nullify(node)
print*, associated(this% root) ! this is true!!
print*, associated(this% root% children) ! this is true!!
end subroutine DESTROY_OCTREE
recursive subroutine CLEAN_NODE ( this )
implicit none
type(t_octree_node) :: this
type(t_octree_node), pointer :: node => null(), next => null()
integer :: i
if( associated(this% leaf) )then
deallocate( this% leaf )
nullify( this% leaf )
endif
if(.not. associated(this% children) )RETURN
do i = 1, 8
node => this% children(i)
deallocate( node )
nullify(node)
! debug
! print*, i, "rec"
enddo
nullify(this% children)
nullify(this% parent)
end subroutine CLEAN_NODE
subroutine DESTROY_LEAF ( leaf )
implicit none
type(t_octree_leaf) :: leaf
deallocate( leaf% id )
end subroutine DESTROY_LEAF
In my main program I do following
program TEST_OCTREE
use MD_OCTREE
implicit none
type(t_octree), pointer :: octree
octree => t_octree( max_num_point, box )
(...) ! all processing to build the data structure
Now I deallocate by simply
deallocate( octree )
print*, associated(octree% root) ! this give a segmentation fault
The question
Can somebody explain why it seems that my print*, associated(this% root)
commands still show TRUE
while when printing in my main program it looks like it has been deallocated as it gives me a segmentation fault