使用指向多个目标的相同指针导致内存泄漏

Memory leaking with using same pointer to numerous target

我的 IDE 是 Visual Studio 2010,带有集成的英特尔 Fortran 编译器。编译器版本为:Intel Parallel Studio XE 2011.

我在 Fortran 方面不是经验丰富的程序员,所以我需要一些关于使用指针从 .txt 文件读取数据的帮助。这是我的示例代码:

Module Derived_type

implicit none

Type , public :: Something

  private

       Real :: Somth_1
    Integer :: Somth_2

  contains

    procedure , public :: read_input => read_data_input
    procedure , public :: t_Somth_1 => t_data_Somth_1
    procedure , public :: t_Somth_2 => t_data_Somth_2

End Type Something

private :: read_data_input
private :: t_data_Somth_1 , t_data_Somth_2

contains

Subroutine read_data_input( This , Un_r )

  Class( Something ) :: This
  Integer , intent( in ) :: Un_r

  Read ( Un_r , * , Err = 100 ) This%Somth_1
  Read ( Un_r , * , Err = 101 ) This%Somth_2

Return

100 Stop ( "Read format error - 100 !!!" )
101 Stop ( "Read format error - 101 !!!" )

End Subroutine read_data_input

Function t_data_Somth_1 ( This ) result( data_Somth_1 )

  Class( Something ) :: This
  Real :: data_Somth_1

  data_Somth_1 = This%Somth_1

End Function t_data_Somth_1

Function t_data_Somth_2 ( This ) result( data_Somth_2 )

  Class( Something ) :: This
  Integer :: data_Somth_2

  data_Somth_2 = This%Somth_2

End Function t_data_Somth_2

End Module Derived_type

Program Memory_leaking

Use , non_intrinsic :: Derived_type

Implicit none

Integer :: i , alloc_err , dealloc_err
Integer , parameter :: N_snv = 3
Character( 256 ) :: Name
Character(*),parameter :: a00 = '("Input_",i1,".txt")'

Class( Something ) , pointer :: Po_Something

Type( Something ) , allocatable , target :: Tar_Something(:)

! Memory allocation

allocate ( Po_Something , Stat = alloc_err )

If ( alloc_err .ne. 0 ) Stop ( "Allocation wrong - Po_Something !!!")

If ( .not. allocated ( Tar_Something ) ) allocate( Tar_Something( N_snv ) , stat = alloc_err )

If ( alloc_err .ne. 0 ) Stop ( "Allocation wrong - Tar_Something !!!")


Do i = 1 , N_snv

   Po_Something => Tar_Something(i)

   Write( Name , a00 ) i

   Open( 15 , File = Name , Status = 'Unknown' , Action = 'Read' )

      Call Po_Something%read_input( 15 )

   Close( 15 , Status = 'Keep' )

   Write(*,*) Po_Something%t_Somth_1() , Po_Something%t_Somth_2()

End Do

! Memory deallocation

deallocate ( Po_Something , Stat = dealloc_err )

If ( dealloc_err .ne. 0 ) Stop ( "deAllocation wrong - Po_Something !!!")

If ( allocated ( Tar_Something ) ) deallocate( Tar_Something, stat = dealloc_err )

If ( dealloc_err .ne. 0 ) Stop ( "deAllocation wrong - Tar_Something !!!")

End program Memory_leaking

我有一个派生类型的数组,我想使用相同的指针从 .txt 文件中读取每个数组的数据,就像在我的示例代码中一样。 我需要在 do 循环执行完成后断开连接 beatwean 指针和目标吗? 在这种情况下是否存在内存泄漏?

是的,你有内存泄漏。

allocate ( Po_Something , Stat = alloc_err )

在这里,您手动为指针变量分配了存储空间,并指向了它的内存地址。

Po_Something => Tar_Something(i)

不久之后,您将 poitee 更改为另一个存储(可分配变量)并留下了您之前分配的存储。在这个指针关联之后,不再有指向您手动分配的地址的引用。当它到达末尾时,程序将无法释放它。

因为你没有使用第一次分配的存储,所以这里的解决方案就是不这样做。该指针将指向已在循环中分配的内存。

可分配变量是自动释放的(在现代 Fortran 中),但是您为指针变量手动分配的任何内存都需要在更改其目标之前手动释放。

查看我的另一个关于何时需要手动释放的答案以供参考。

(更好的是,在此处下载 Fortran 标准的副本以供参考 https://wg5-fortran.org