Fortran array of derived types and memory leaks despite finalization

后端 未结 1 909
无人共我
无人共我 2020-11-28 16:20

I defined a derived type and encountered some problems with memory deallocation although I had written the final procedure. The code is as follows

module Mod         


        
相关标签:
1条回答
  • 2020-11-28 16:38

    In the description of the finalization process we see (Fortran 2008, 4.5.6.2)

    If the dynamic type of the entity has a final subroutine whose dummy argument has the same kind type parameters and rank as the entity being finalized, it is called with the entity as an actual argument. Otherwise, if there is an elemental final subroutine whose dummy argument has the same kind type parameters as the entity being finalized, it is called with the entity as an actual argument. Otherwise, no subroutine is called at this point.

    There is a final subroutine for the derived type provided only for scalar (rank-0) entities. To have finalization for your rank-1 entity the simplest way (it seems, in this case) is to make the subroutine you have elemental.

    I'm slightly reluctant to mention the =>NULL() aspect as I have no current means of testing what I'm about to write, but I'll speculate.

    Without the =>NULL() default initialization the pointer component has undefined association status. This means, that when you do

        b1=TCoordinate(1.0_8,1.0_8)
    

    interesting things happen.

    As part of the assignment b1 is finalized on entry to TCoordinateAssignment. The finalization involves calling associated with the pointer which is of undefined association status. This is not allowed (with the consequence that any result could come about).

    0 讨论(0)
提交回复
热议问题