Bug 41586 - Allocatable _scalars_ are never auto-deallocated
Summary: Allocatable _scalars_ are never auto-deallocated
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.5.0
: P3 normal
Target Milestone: ---
Assignee: janus
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2009-10-05 13:26 UTC by Tobias Burnus
Modified: 2009-10-19 19:23 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2009-10-11 16:44:18


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2009-10-05 13:26:57 UTC
(Cf. also PR 41585)

type t0
end type t0
type t
  integer :: i
  class(t0), allocatable :: foo
end type t
type(t) :: m
allocate(t0 :: m%foo)
m%i = 5
end

leaks memory:

    D.1375 = (void * restrict) __builtin_malloc (1);
    m.foo.$data = (struct t0 *) D.1375;
  m.foo.$vindex = 1;
  m.i = 5;
}


SAME FOR:

type t0
end type t0
class(t0), allocatable :: m
allocate(t0 :: m)
end

AND ALSO FOR:

type t0
end type t0
type(t0), allocatable :: m(:)
allocate(t0 :: m(3))
end
Comment 1 Tobias Burnus 2009-10-05 15:17:23 UTC
Does not need to be a component or derived type, any scalar leaks:

integer , allocatable :: a
allocate(a)
a = 42
end


MAIN__ ()
{
  integer(kind=4) * a;
  {
    void * restrict D.1366;
    D.1366 = (void * restrict) __builtin_malloc (4);
    if (D.1366 == 0B)
      {
        _gfortran_os_error (&"Out of memory"[1]{lb: 1 sz: 1});
      }
    a = (integer(kind=4) *) D.1366;
  }
  *a = 42;
}
Comment 2 Thomas Koenig 2009-10-11 16:44:17 UTC
Confirmed.
Comment 3 janus 2009-10-16 21:04:41 UTC
> AND ALSO FOR:
> 
> type t0
> end type t0
> type(t0), allocatable :: m(:)
> allocate(t0 :: m(3))
> end

No, this one actually works (since 'm' is not a scalar):

  if (m.data != 0B)
    {
      __builtin_free ((void *) m.data);
    }
  m.data = 0B;
Comment 4 janus 2009-10-19 18:46:17 UTC
Mine. Have a patch:

http://gcc.gnu.org/ml/fortran/2009-10/msg00171.html
Comment 5 janus 2009-10-19 19:21:31 UTC
Subject: Bug 41586

Author: janus
Date: Mon Oct 19 19:21:18 2009
New Revision: 152988

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=152988
Log:
2009-10-19  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/41586
	* parse.c (parse_derived): Correctly set 'alloc_comp' and 'pointer_comp'
	for CLASS variables.
	* trans-array.c (structure_alloc_comps): Handle deallocation and
	nullification of allocatable scalar components.
	* trans-decl.c (gfc_get_symbol_decl): Remember allocatable scalars for
	automatic deallocation.
	(gfc_trans_deferred_vars): Automatically deallocate allocatable scalars.


2009-10-19  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/41586
	* gfortran.dg/auto_dealloc_1.f90: New test case.

Added:
    trunk/gcc/testsuite/gfortran.dg/auto_dealloc_1.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/parse.c
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/testsuite/ChangeLog

Comment 6 janus 2009-10-19 19:23:16 UTC
Fixed with r152988. Closing.