我正在寻找一种在Fortran 2008中使用用户定义的类型来构建树结构的方法。虽然我可以使用一些基本代码,但遇到内存泄漏,无法查明。
树结构不必过于通用,因为它被用作一次性插入和多次读取类型的存储,这就是我决定使用可分配表的原因。由于Fortran不允许将可分配类型用作其自己的成员之一,因此我正在使用指针引用的中间结构来存储该可分配地址。因此,以下是我想使用的,但不被允许使用:
type :: invalid_section
type(invalid_section), dimension(:), allocatable :: subsections
end type
在下面的示例中,我使用了惰性指针的分配(仅在有要添加/存在的子节点时分配它),该指针指向持有可分配类型的指针。
module sectiontest
type :: section
type(subsections), pointer :: subsections_ => null()
contains
procedure, pass(self) :: section_assign
generic :: assignment(=) => section_assign
final :: section_cleanup, section_cleanup_arr
end type
type :: subsections
type(section), dimension(:), allocatable :: arr
end type
interface section
module procedure constructor
end interface
contains
type(section) function constructor(subsections)
type(section), optional, intent(in) :: subsections(:)
integer :: idx
print *, "constructor"
if (present(subsections)) then
print *, "allocating subsection"
allocate(constructor%subsections_)
allocate(constructor%subsections_%arr(size(subsections)))
do idx=1,size(subsections)
! make sure we recursively copy everything
constructor%subsections_%arr(idx) = subsections(idx)
enddo
endif
end function
recursive subroutine section_assign(self, rhs)
class(section), intent(inout) :: self
type(section), intent(in) :: rhs
integer :: idx
print *, "assign"
if (associated(self%subsections_)) then
deallocate(self%subsections_)
endif
if (associated(rhs%subsections_)) then
print *, "allocation subsection"
allocate(self%subsections_)
allocate(self%subsections_%arr(size(rhs%subsections_%arr)))
do idx=1,size(rhs%subsections_%arr)
self%subsections_%arr(idx) = rhs%subsections_%arr(idx)
enddo
endif
end subroutine
recursive subroutine section_cleanup(sec)
type(section), intent(inout) :: sec
print *, "section_cleanup"
if (associated(sec%subsections_)) then
print *, " deallocated a subsection"
deallocate(sec%subsections_)
endif
end subroutine
recursive subroutine section_cleanup_arr(arr)
type(section), dimension(:), intent(inout) :: arr
integer :: idx
print *, "deallocating array of sections of size:", size(arr)
do idx=1,size(arr)
print *, "deallocating subsection array index", idx
if (associated(arr(idx)%subsections_)) then
print *, " deallocated a subsection"
deallocate(arr(idx)%subsections_)
endif
end do
end subroutine
subroutine demo()
type(section) :: root
root = section(subsections=[ &
section(subsections=[section(), section(), section()]), &
section() &
])
end subroutine
end module sectiontest
program main
use sectiontest
implicit none
call demo()
end program
从gfortran
(7和9),flang
和nagfor
中,我得到了allocate(constructor%subsections_)
中constructor
引发的直接内存泄漏。
来自gfortran-7
并使用-fsanitize=address
构建:
==26536==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 48 byte(s) in 1 object(s) allocated from:
#0 0x7f965539c510 in malloc (/usr/lib64/libasan.so.4+0xdc510)
#1 0x407e35 in __sectiontest_MOD_constructor /users/tiziano/work/tests/fortran/cp2k_input_parser/recursive_mwe.f90:31
#2 0x40432a in __sectiontest_MOD_demo /users/tiziano/work/tests/fortran/cp2k_input_parser/recursive_mwe.f90:92
#3 0x4090d9 in MAIN__ /users/tiziano/work/tests/fortran/cp2k_input_parser/recursive_mwe_prog.f90:5
#4 0x409119 in main /users/tiziano/work/tests/fortran/cp2k_input_parser/recursive_mwe_prog.f90:2
#5 0x7f96543c2f89 in __libc_start_main (/lib64/libc.so.6+0x20f89)
我正在寻找替代实现(但最好是类似的优雅初始化)或对内存泄漏的解释和可能的解决方案。
答案 0 :(得分:3)
Fortran 2008支持包含所定义类型的可分配组件的类型。这将代码简化为:
module sectiontest
type :: section
type(section), allocatable :: subsections(:)
end type
contains
subroutine demo()
type(section) :: root
root = section( subsections=[ &
section(subsections=[section(), section(), section()]), &
section() ])
end subroutine
end module sectiontest
program main
use sectiontest
implicit none
call demo()
end program
gfortran的最新版本支持此语言功能。
对于未充分支持Fortran 2008的编译器,问题中的代码是合理的解决方法,并且将在正确实现Fortran 2003的编译器上工作。
但是,gfortran(至少达到9.1.1)无法正确实现函数结果的最终确定-因此观察到内存泄漏。