基础: 我正在尝试用Fortran编写优秀的代码,使用构造函数和析构函数。以下是一个非常简单的Test
类及其客户端示例:
module test_class_module
implicit none
type :: Test
private
integer, allocatable :: arr(:)
CONTAINS
final :: destructor
end type
interface Test
procedure :: constructor
end interface
CONTAINS
function constructor(arr_size) result(this)
type(Test) :: this
integer, intent(in) :: arr_size
write(*,*) 'Constructor works'
allocate(this % arr(arr_size))
end function
subroutine destructor(this)
type(Test) :: this
write(*,*) 'Destructor works'
if (ALLOCATED(this % arr)) deallocate(this % arr)
end subroutine
end module
program test_client
use test_class_module
type(Test) :: tst
tst = Test(100)
end
问题:
我使用 valgrind
运行它,并打印出以下结果:
Constructor works
Destructor works
Destructor works
==22229== HEAP SUMMARY:
==22229== in use at exit: 432 bytes in 2 blocks
==22229== total heap usage: 10 allocs, 8 frees, 13,495 bytes allocated
问题:为什么内存仍然被分配?(注:我理解赋值运算符在正确使用类时的必要性,但这对于此问题不足够)感谢任何想法。