我的带有派生类型指针的代码是否会泄漏内存? [英] Does my code with derived type pointers leak memory?

查看:40
本文介绍了我的带有派生类型指针的代码是否会泄漏内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的IDE是带有编译器GFortran 6.3.1的Code :: Blocks 17.2

My IDE is Code::Blocks 17.2 with compiler GFortran 6.3.1

所有代码为:

PROGRAM EES_TEST

USE , NON_INTRINSIC :: DERIVED_TYPE

IMPLICIT NONE

INTEGER :: I , ALLOC_ERR , DEALLOC_ERR
LOGICAL :: GLOBAL_ERR

CLASS ( TRONA ) , POINTER :: P_TRA
TYPE ( TRONA ) , ALLOCATABLE , TARGET :: TRAN(:)

IF ( .NOT. ALLOCATED ( TRAN ) ) ALLOCATE ( TRAN ( 2 ) , STAT = ALLOC_ERR )

IF ( ALLOC_ERR .NE. 0 ) STOP ("PROBLEM WITH MEMORY ALLOCATION - TRAN!!!")

OPEN ( UNIT = 15 , FILE = 'INPUT.TXT' , ACTION = 'READ' )

  DO I = 1 , 2

     P_TRA => TRAN ( I )
     GLOBAL_ERR = P_TRA%UCI()

     IF ( GLOBAL_ERR .EQV. .TRUE. ) STOP ("ERROR WITH READING FROM OUTPUT.TXT!")

  END DO

CLOSE ( 15 )

IF ( ALLOCATED ( TRAN ) ) DEALLOCATE ( TRAN , STAT = DEALLOC_ERR )

IF ( DEALLOC_ERR .NE. 0 ) STOP ("PROBLEM WITH MEMORY DEALLOCATION - TRAN!!!")

END PROGRAM EES_TEST

MODULE DERIVED_TYPE

IMPLICIT NONE

TYPE , PUBLIC :: TRONA

  PRIVATE

    REAL :: Sn
    REAL :: Vn

  CONTAINS

    PROCEDURE , PUBLIC :: UCI => UCI_POD_TRONA
    PROCEDURE , PUBLIC :: TAKE_Sn => TAKE_POD_Sn
    PROCEDURE , PUBLIC :: TAKE_Vn => TAKE_POD_Vn

END TYPE TRONA

PRIVATE :: UCI_POD_TRONA
PRIVATE :: TAKE_POD_Sn , TAKE_POD_Vn

CONTAINS

  FUNCTION UCI_POD_TRONA ( THIS ) RESULT ( WRONG )

    IMPLICIT NONE

    CLASS ( TRONA ) :: THIS
    LOGICAL :: WRONG

    WRONG = .FALSE.

    READ ( 15 , * , ERR = 100 ) THIS%Sn
    READ ( 15 , * , ERR = 101 ) THIS%Vn

  RETURN

  100 WRITE (*,*) "WRONG FORMAT - INPUT 100!"
  WRONG = .TRUE.
  STOP

  101 WRITE (*,*) "WRONG FORMAT - INPUT 101!"
  WRONG = .TRUE.
  STOP

  END FUNCTION UCI_POD_TRONA

  FUNCTION TAKE_POD_Sn ( THIS ) RESULT ( POD_Sn )

    IMPLICIT NONE

    CLASS ( TRONA ) :: THIS
    REAL :: POD_Sn

    POD_Sn = THIS%Sn

  RETURN
  END FUNCTION TAKE_POD_Sn


  FUNCTION TAKE_POD_Vn ( THIS ) RESULT ( POD_Vn )

    IMPLICIT NONE

    CLASS ( TRONA ) :: THIS
    REAL :: POD_Vn

    POD_Vn = THIS%Vn

  RETURN
  END FUNCTION TAKE_POD_Vn

END MODULE DERIVED_TYPE

我在Fortran中的面向对象编程中是一个非常新的人,因此我需要一个关于使用对象指针从派生类型调用方法的解释.在这种情况下,我想检查内存泄漏是否存在任何问题,如果是这种情况,是否存在用于检查丢失了多少内存以及在哪一行中进行检查的方法?另一件事是使派生类型指针无效.在这种情况下该怎么办?

I am very new in object oriented programing in Fortran so I need an explanation about using the object pointer for calling methods from derived types. In this case I want to check is there any problem with memory leaking and if it is case is there method for checking how much memory was lost and in which line? Another thing is nullifying the derived type pointer. How to do that for this case?

推荐答案

通常,除非在某处分配了某些内容,否则不会发生内存泄漏.根本不可能.您只能泄漏分配给指针目标的内容,而不能泄漏其他任何内容.

Generally, unless you allocate something somewhere, you cannot have memory leaks. It is simply impossible. You can only leak something you allocate as a pointer target, nothing else.

在您的代码中,指针没有 allocate(),因此不会发生任何内存泄漏.

In your code you have no allocate() for a pointer, so there cannot be any memory leaks.

要发生内存泄漏,必须依次发生两件事.

For a memory leak to happen, two things must happen in sequence.

  1. 必须分配一个匿名指针目标.只有通过allocate语句才有可能

  1. An anonymous pointer target must be allocated. That is possible only through the allocate statement

allocate(p_tra)

  • 指向目标的指针丢失.要么将其重定向到其他地方

  • The pointer to the target is lost. Either it is redirected somewhere else

    p_tra => somewhere_else
    

    或者它不再存在,因为它是完成的子例程的局部变量,或者是被释放的结构或类似结构的组成部分...

    Or it ceases to exist, because it is a local variable of a subroutine that finishes or it is a component of a structure which is deallocated or similar...

    您始终可以使用GCC清理 -fssnitize = leak valgrind 来检查内存泄漏.

    You can always use GCC sanitizations -fssnitize=leak or valgrind to check for memory leaks.

    关于nulifying,只需使用 nulify 语句或将其分配给 null().它是一个和其他指针一样的指针.

    Regarding the nulifying, just use the nulify statement or assign to null(). It is a pointer like any other.

    p_tra => null()
    

    这篇关于我的带有派生类型指针的代码是否会泄漏内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆