Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop Fortran中最终例程的正确执行_Oop_Fortran_Intel Fortran_Finalizer - Fatal编程技术网

Oop Fortran中最终例程的正确执行

Oop Fortran中最终例程的正确执行,oop,fortran,intel-fortran,finalizer,Oop,Fortran,Intel Fortran,Finalizer,我有下面的Fortran代码 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

我有下面的Fortran代码

  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
在我完成处理之后,现在需要确保我的东西被正确地释放

在我的最后一套动作中

  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
在我的主程序中,我做以下工作

  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
现在我通过简单的方法解除分配

  deallocate( octree )  
  print*, associated(octree% root) ! this give a segmentation fault
问题
有人能解释为什么我的
print*,关联(这个%root)
命令仍然显示
TRUE
而在我的主程序中打印时,它看起来像是被释放了,因为它给我一个分段错误

释放指针会导致与同一目标关联的任何其他指针的指针关联状态变得未定义(F2018 9.7.3.3p2)。这种情况发生在
DESTROY_OCTREE
过程中的
node
This%root
中-两个指针引用相同的对象(通过指针分配
节点=>此%root
,对象通过
节点
指针解除分配-这使得
此%root
具有未定义的关联状态

关联内在函数的参数不能是具有未定义关联状态(F2018 16.9.16p3)的指针。代码不符合要求,任何事情都可能发生-其中“任何事情”都合理地包括您看到的结果

当您通过
节点
指针取消分配对象时,处理器无法通过简单的方式可靠地更新
此%root
指针的状态-它最终指向不再存在的对象


在显示的源代码片段中还有其他可疑的构造,包括在同一指针对象上的DEALLOCATE语句之后使用多余的null(成功的deallocation解除指针的关联(null化),在DEALLOCATE合适时使用null(很难说没有完整的代码),并使用似乎是结构构造函数的对象作为指针目标。

谢谢你的回答。换句话说,我的析构函数基本上已解除分配了我的树结构。我明白你关于“解除分配后为空”语句的意思。还有什么可疑的?还有,你所说的代码不一致是什么意思?OP创建的构造函数s作为分配的指针函数。我试图对其进行压缩,这是一种不好的做法,但将构造函数用作指针目标可能与此有关。@VladimirF,你指的是我的
octree=>t_octree(max_num_point,box)
其中
octree
是一个指针。如果是,那么这就是我的问题所在:我以前做过的事,就是做
octree=t_八叉树(max_num_point,方框)
,但一旦构造完成,就会执行销毁,
final
。当有一个指针时,我可以通过显式的DEALLOCATE语句来控制调用
final
,所以当我调用
octree=t_octree时(max_num_point,box)
,也执行了
最终
例程,但在程序退出之前它没有执行