递归类型的内部组件不会在内部分配中复制

问题描述

考虑以下Fortran程序:

program test_prg
  implicit none

  type :: recursive_block
    type(recursive_block),allocatable :: subblocks(:,:)
  end type

  type(recursive_block) :: top_level
  top_level = init_recursive()
  print *,loc(top_level % subblocks(1,1) % subblocks)
  deallocate(top_level % subblocks)

contains

  function init_recursive() result(res)
    type(recursive_block) :: res
    type(recursive_block),allocatable :: inner(:,:)

    allocate(inner(1,1))
    allocate(inner(1,1) % subblocks(1,1))
    print *,loc(inner(1,1) % subblocks)
    res % subblocks = inner
  end function
end program

与gfortran 10.2一起编译,此代码deallocate(top_level % subblocks)上崩溃,并显示以下消息:

free(): double free detected in tcache 2

我怀疑这可能与以下事实有关:两个打印语句都打印相同的数字,即top_level % subblocks(1,1) % subblocks的地址与inner(1,1) % subblocks的地址匹配,后者是局部变量的组成部分,应该在函数退出时被释放。

这是gfortran错误吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)