Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Memory leak with Fortran subroutine

stankoz
Beginner
5,245 Views

Hello,

I have converted a publicly available fortran simulation into a subroutine within a C project. I am encountering steadily increasing memory usage with each call to the routine. Running Inspector with the 'memory leaks' option produced no problems. I think there may be an issue with the use of pointers and the SAVE declaration attribute within a module of the subroutine. Do I need to manually deallocate every allocatable array and targets of pointers before returning from the subroutine? What is the most thorough way of clearing up all memory usage at the end of each simulation run? All local allocatable arrays will be deallocated once the routine exits, correct?

Here is an example of the module:

      MODULE VDFMODULE
        INTEGER, SAVE, POINTER              ::MT3DRHOFLG
        INTEGER, SAVE, POINTER              ::MFNADVFD
        INTEGER, SAVE, POINTER              ::NSWTCPL
        INTEGER, SAVE, POINTER              ::IWTABLE
        INTEGER, SAVE, POINTER              ::NSRHOEOS
        REAL,    SAVE, POINTER              ::DENSEMIN        
        REAL,    SAVE, POINTER              ::DENSEMAX
        REAL,    SAVE, POINTER              ::DENSEREF
        REAL,    SAVE, POINTER              ::FIRSTDT
        REAL,    SAVE, POINTER              ::DNSCRIT
        REAL,    SAVE, POINTER              ::DRHODPRHD
        REAL,    SAVE, POINTER              ::PRHDREF
        REAL,    SAVE, POINTER              ::HDRY
        REAL,    SAVE, POINTER              ::HNOFLO
        INTEGER, SAVE, POINTER,  DIMENSION(:)                ::MTRHOSPEC
        REAL,    SAVE, POINTER,  DIMENSION(:,:,:)            ::PS
        REAL,    SAVE, POINTER,  DIMENSION(:,:,:)            ::RHOCR
        REAL,    SAVE, POINTER,  DIMENSION(:,:,:)            ::RHOCC
        REAL,    SAVE, POINTER,  DIMENSION(:,:,:)            ::RHOCV
        REAL,    SAVE, POINTER,  DIMENSION(:,:,:)            ::HSALT
        REAL,    SAVE, POINTER,  DIMENSION(:,:,:)            ::ELEV
        REAL,    SAVE, POINTER,  DIMENSION(:,:,:)            ::DCDT
        REAL,    SAVE, POINTER,  DIMENSION(:,:,:,:)          ::COLDFLW
        REAL,    SAVE, POINTER,  DIMENSION(:,:,:)            ::PSOLDITER
        REAL,    SAVE, POINTER,  DIMENSION(:)                ::DRHODC
        REAL,    SAVE, POINTER,  DIMENSION(:)                ::CRHOREF
      TYPE VDFTYPE
        INTEGER, POINTER              ::MT3DRHOFLG
        INTEGER, POINTER              ::MFNADVFD
        INTEGER, POINTER              ::NSWTCPL
        INTEGER, POINTER              ::IWTABLE
        INTEGER, POINTER              ::NSRHOEOS
        REAL,    POINTER              ::DENSEMIN        
        REAL,    POINTER              ::DENSEMAX
        REAL,    POINTER              ::DENSEREF
        REAL,    POINTER              ::FIRSTDT
        REAL,    POINTER              ::DNSCRIT
        REAL,    POINTER              ::DRHODPRHD
        REAL,    POINTER              ::PRHDREF
        REAL,    POINTER              ::HDRY
        REAL,    POINTER              ::HNOFLO
        INTEGER, POINTER,  DIMENSION(:)                ::MTRHOSPEC
        REAL,    POINTER,  DIMENSION(:,:,:)            ::PS
        REAL,    POINTER,  DIMENSION(:,:,:)            ::RHOCR
        REAL,    POINTER,  DIMENSION(:,:,:)            ::RHOCC
        REAL,    POINTER,  DIMENSION(:,:,:)            ::RHOCV
        REAL,    POINTER,  DIMENSION(:,:,:)            ::HSALT
        REAL,    POINTER,  DIMENSION(:,:,:)            ::ELEV
        REAL,    POINTER,  DIMENSION(:,:,:)            ::DCDT
        REAL,    POINTER,  DIMENSION(:,:,:,:)          ::COLDFLW
        REAL,    POINTER,  DIMENSION(:,:,:)            ::PSOLDITER
        REAL,    POINTER,  DIMENSION(:)                ::DRHODC
        REAL,    POINTER,  DIMENSION(:)                ::CRHOREF
      END TYPE
      TYPE(VDFTYPE),SAVE:: VDFDAT(10)
      END MODULE VDFMODULE

Thank you

0 Kudos
21 Replies
IanH
Honored Contributor III
396 Views
The specific use case for the OP, where you have a scalar integer pointer, is something that would be worth understanding because it doesn't make sense in the context of a wrapper for code with F77 heritage (F77 not having pointers...). But as a general point, a pointer can be used to indicate three things to code: 1. the thing I want the code to work on doesn't exist at all (pointer => NULL()). 2. the thing I want the code to work on is part (perhaps the entire part) of some existing object ( pointer => existing_object(xx)%yy ) 3. the thing I want the code to work on is its own stand-alone object (ALLOCATE(pointer)) Those three cases apply equally to scalars as arrays. There was a fourth pre F2008: 4. Language rules won't let me do what I want to do with allocatables (can't have them as scalars, components or dummy arguments pre F2003, can't forward reference them pre F2008). (Note that scalars are no longer all fixed size in F2003.) Perhaps there are more.
0 Kudos
Reply