From hebert@ajax.serma.cea.fr  Thu Dec  7 11:28:00 1995
Received: from trefle.saclay.cea.fr (trefle.saclay.cea.fr [132.166.192.212]) by dkuug.dk (8.6.12/8.6.12) with ESMTP id LAA19924 for <sc22wg5@dkuug.dk>; Thu, 7 Dec 1995 11:27:46 +0100
From: hebert@ajax.serma.cea.fr
Received: from oeillet.saclay.cea.fr by trefle.saclay.cea.fr
	(8.6.10/ CEANET-ROUTER-3.0) with ESMTP id LAA15903
	for <sc22wg5@dkuug.dk>; Thu, 7 Dec 1995 11:26:43 +0100
Received: from ajax by oeillet.saclay.cea.fr
	(8.6.10/ CEANET-ROUTER-3.0) with SMTP id LAA05533
	for <sc22wg5@dkuug.dk>; Thu, 7 Dec 1995 11:27:36 +0100
Message-Id: <199512071027.LAA05533@oeillet.saclay.cea.fr>
Received: by ajax
	(1.38.193.4/CEANET.2.0.1) id AA09960; Thu, 7 Dec 1995 11:29:49 +0100
Subject: Major flaw in Fortran-95 standard -> memory leaking
To: sc22wg5@dkuug.dk
Date: Thu, 7 Dec 95 11:29:47 MET
Mailer: Elm [revision: 70.85]

I discovered a major flaw in the Fortran-90 standard which is
succeptible to cause memory leakage when derived types are allocated
with the POINTER attribute. The simplest way to illustrate this
problem is to use the ISO_VARYING_STRING module (ISO/IEC 1539-2)
and to code

      USE ISO_VARYING_STRING
      TYPE(VARYING_STRING),POINTER :: TEXT
      ALLOCATE(TEXT)
      TEXT='ABCDE'
      DEALLOCATE(TEXT)

This simple example causes a memory leak due to the internal
definition of the type VARYING_STRING:

TYPE VARYING_STRING
 PRIVATE
 CHARACTER,DIMENSION(:),POINTER :: chars
ENDTYPE VARYING_STRING

The DEALLOCATE(TEXT) statement does not deallocate the char array.
This behavior is typical of most derived types containing pointers
components, and memory leakage is likely to occurs in all these cases.

A solution to this problem would be to add the possibility to
overload ALLOCATE and DEALLOCATE statements in Fortran-95 or 2000.
In the ISO_VARYING_STRING module, one could write:

...
!----- specification of publically accessible entities -----------------------!
PUBLIC :: VARYING_STRING,VAR_STR,CHAR,LEN,GET,PUT,PUT_LINE,INSERT,REPLACE,   &
          SPLIT,REMOVE,REPEAT,EXTRACT,INDEX,SCAN,VERIFY,LLT,LLE,LGE,LGT,     &
          ASSIGNMENT(=),OPERATOR(//),OPERATOR(==),OPERATOR(/=),OPERATOR(<),  &
          OPERATOR(<=),OPERATOR(>=),OPERATOR(>),LEN_TRIM,TRIM,IACHAR,ICHAR,  &
          ADJUSTL,ADJUSTR,STATEMENT(DEALLOCATE)
                          ^^^^^^^^^^^^^^^^^^^^^
...

!----- Deallocate statement --------------------------------------------------!
INTERFACE STATEMENT(DEALLOCATE)
  MODULE PROCEDURE dealloc_s,dealloc_v
ENDINTERFACE

...
 SUBROUTINE dealloc_s(string_a)
  type(VARYING_STRING),POINTER :: string_a
  IF(ASSOCIATED(string_a%char))DEALLOCATE(string_a%char)
  DEALLOCATE(string_a)
 ENDSUBROUTINE dealloc_s
!
 SUBROUTINE dealloc_v(string_a)
  type(VARYING_STRING),POINTER,DIMENSION(:) :: string_a
  DO I=1,SIZE(string_a)
     IF(ASSOCIATED(string_a(I)%char))DEALLOCATE(string_a(I)%char)
  ENDDO
  DEALLOCATE(string_a)
 ENDSUBROUTINE dealloc_v
...

Any suggestion would be appreciated.

Alain Hebert
Commissariat a l'Energie Atomique
France
(1) 69 08 27 44



