From dbb7247b9a209ac3afef3c5174ba552c1f941bec Mon Sep 17 00:00:00 2001 From: Janus Weil Date: Mon, 27 Jun 2011 22:59:12 +0200 Subject: [PATCH] re PR fortran/49466 (Memory leak with assignment of extended derived types) 2011-06-27 Janus Weil PR fortran/49466 * trans-array.c (structure_alloc_comps): Make sure sub-components and extended types are correctly deallocated. 2011-06-27 Janus Weil PR fortran/49466 * gfortran.dg/allocatable_scalar_9.f90: Modified. * gfortran.dg/extends_14.f03: Modified. From-SVN: r175563 --- gcc/fortran/ChangeLog | 6 ++++ gcc/fortran/trans-array.c | 20 +++++++------ gcc/testsuite/ChangeLog | 6 ++++ .../gfortran.dg/allocatable_scalar_9.f90 | 2 +- gcc/testsuite/gfortran.dg/extends_14.f03 | 28 +++++++++++++++++++ 5 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/extends_14.f03 diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index b1f48537632..ed9c705691c 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,9 @@ +2011-06-27 Janus Weil + + PR fortran/49466 + * trans-array.c (structure_alloc_comps): Make sure sub-components + and extended types are correctly deallocated. + 2011-06-21 Andrew MacLeod * trans-openmp.c: Add sync_ or SYNC__ to builtin names. diff --git a/gcc/fortran/trans-array.c b/gcc/fortran/trans-array.c index baf9060fe6b..408b73a9a68 100644 --- a/gcc/fortran/trans-array.c +++ b/gcc/fortran/trans-array.c @@ -6682,18 +6682,22 @@ structure_alloc_comps (gfc_symbol * der_type, tree decl, switch (purpose) { case DEALLOCATE_ALLOC_COMP: + if (cmp_has_alloc_comps && !c->attr.pointer) + { + /* Do not deallocate the components of ultimate pointer + components. */ + comp = fold_build3_loc (input_location, COMPONENT_REF, ctype, + decl, cdecl, NULL_TREE); + rank = c->as ? c->as->rank : 0; + tmp = structure_alloc_comps (c->ts.u.derived, comp, NULL_TREE, + rank, purpose); + gfc_add_expr_to_block (&fnblock, tmp); + } + if (c->attr.allocatable && c->attr.dimension) { comp = fold_build3_loc (input_location, COMPONENT_REF, ctype, decl, cdecl, NULL_TREE); - if (cmp_has_alloc_comps && !c->attr.pointer) - { - /* Do not deallocate the components of ultimate pointer - components. */ - tmp = structure_alloc_comps (c->ts.u.derived, comp, NULL_TREE, - c->as->rank, purpose); - gfc_add_expr_to_block (&fnblock, tmp); - } tmp = gfc_trans_dealloc_allocated (comp); gfc_add_expr_to_block (&fnblock, tmp); } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 6751872c9ae..dd426210120 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2011-06-27 Janus Weil + + PR fortran/49466 + * gfortran.dg/allocatable_scalar_9.f90: Modified. + * gfortran.dg/extends_14.f03: Modified. + 2011-06-27 Janis Johnson * lib/target-supports-dg.exp (dg-require-effective-target): Return diff --git a/gcc/testsuite/gfortran.dg/allocatable_scalar_9.f90 b/gcc/testsuite/gfortran.dg/allocatable_scalar_9.f90 index f4c6599b02c..fef9b053275 100644 --- a/gcc/testsuite/gfortran.dg/allocatable_scalar_9.f90 +++ b/gcc/testsuite/gfortran.dg/allocatable_scalar_9.f90 @@ -49,7 +49,7 @@ if(allocated(na3%b3)) call abort() if(allocated(na4%b4)) call abort() end -! { dg-final { scan-tree-dump-times "__builtin_free" 32 "original" } } +! { dg-final { scan-tree-dump-times "__builtin_free" 38 "original" } } ! { dg-final { cleanup-tree-dump "original" } } ! { dg-final { cleanup-modules "m" } } diff --git a/gcc/testsuite/gfortran.dg/extends_14.f03 b/gcc/testsuite/gfortran.dg/extends_14.f03 new file mode 100644 index 00000000000..876e8c703cf --- /dev/null +++ b/gcc/testsuite/gfortran.dg/extends_14.f03 @@ -0,0 +1,28 @@ +! { dg-do compile } +! { dg-options "-fdump-tree-original" } +! +! PR 49466: [4.6/4.7 Regression] Memory leak with assignment of extended derived types +! +! Contributed by Rich Townsend + +program evolve_aflow + + implicit none + + type :: state_t + real, allocatable :: U(:) + end type + + type, extends(state_t) :: astate_t + end type + + type(astate_t) :: a,b + + allocate(a%U(1000)) + + a = b + +end program + +! { dg-final { scan-tree-dump-times "__builtin_free" 3 "original" } } +! { dg-final { cleanup-tree-dump "original" } } -- 2.30.2