我正在寻找一种在 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)
我正在寻找替代实现(但最好是类似的优雅初始化)或内存泄漏的解释和可能的解决方案。