From 1b961de9db0b7ffb8d7f4614ddf61faf31a544a5 Mon Sep 17 00:00:00 2001 From: Paul Thomas Date: Tue, 26 May 2015 10:20:41 +0000 Subject: [PATCH] re PR fortran/66082 (memory leak with automatic array dummy argument with derived type array constructor actual argument) 2015-05-26 Paul Thomas PR fortran/66082 * trans-array.c (gfc_conv_array_parameter): Ensure that all non-variable arrays with allocatable components have the components deallocated after the procedure call. 2015-05-26 Paul Thomas PR fortran/66082 * gfortran.dg/allocatable_scalar_13.f90: New test From-SVN: r223677 --- gcc/fortran/ChangeLog | 7 +++++ gcc/fortran/trans-array.c | 13 +++++++- gcc/testsuite/ChangeLog | 5 ++++ .../gfortran.dg/alloc_comp_auto_array_3.f90 | 30 +++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gfortran.dg/alloc_comp_auto_array_3.f90 diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index bc623c4dbde..0bada49d283 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,10 @@ +2015-05-26 Paul Thomas + + PR fortran/66082 + * trans-array.c (gfc_conv_array_parameter): Ensure that all + non-variable arrays with allocatable components have the + components deallocated after the procedure call. + 2015-05-24 Mikael Morin PR fortran/66257 diff --git a/gcc/fortran/trans-array.c b/gcc/fortran/trans-array.c index e6edf742377..c8fab454249 100644 --- a/gcc/fortran/trans-array.c +++ b/gcc/fortran/trans-array.c @@ -4458,7 +4458,7 @@ gfc_conv_resolve_dependencies (gfc_loopinfo * loop, gfc_ss * dest, if (!nDepend && dest_expr->rank > 0 && dest_expr->ts.type == BT_CHARACTER && ss_expr->expr_type == EXPR_VARIABLE) - + nDepend = gfc_check_dependency (dest_expr, ss_expr, false); continue; @@ -7267,6 +7267,17 @@ gfc_conv_array_parameter (gfc_se * se, gfc_expr * expr, bool g77, if (no_pack || array_constructor || good_allocatable || ultimate_alloc_comp) { gfc_conv_expr_descriptor (se, expr); + /* Deallocate the allocatable components of structures that are + not variable. */ + if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS) + && expr->ts.u.derived->attr.alloc_comp + && expr->expr_type != EXPR_VARIABLE) + { + tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, se->expr, expr->rank); + + /* The components shall be deallocated before their containing entity. */ + gfc_prepend_expr_to_block (&se->post, tmp); + } if (expr->ts.type == BT_CHARACTER) se->string_length = expr->ts.u.cl->backend_decl; if (size) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index aeb7308fbeb..ccfca6add59 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2015-05-26 Paul Thomas + + PR fortran/66082 + * gfortran.dg/allocatable_scalar_13.f90: New test + 2015-05-25 Eric Botcazou * gnat.dg/warn11.adb: New test. diff --git a/gcc/testsuite/gfortran.dg/alloc_comp_auto_array_3.f90 b/gcc/testsuite/gfortran.dg/alloc_comp_auto_array_3.f90 new file mode 100644 index 00000000000..57835131f1f --- /dev/null +++ b/gcc/testsuite/gfortran.dg/alloc_comp_auto_array_3.f90 @@ -0,0 +1,30 @@ +! { dg-do compile } +! { dg-options "-fdump-tree-original" } +! +! Test the fix for PR66082. The original problem was with the first +! call foo_1d. +! +! Reported by Damian Rouson +! + type foo_t + real, allocatable :: bigarr + end type + block + type(foo_t) :: foo + allocate(foo%bigarr) + call foo_1d (1,[foo]) ! wasy lost + call foo_1d (1,bar_1d()) ! Check that this is OK + end block +contains + subroutine foo_1d (n,foo) + integer n + type(foo_t) :: foo(n) + end subroutine + function bar_1d () result (array) + type(foo_t) :: array(1) + allocate (array(1)%bigarr) + end function +end +! { dg-final { scan-tree-dump-times "builtin_malloc" 3 "original" } } +! { dg-final { scan-tree-dump-times "builtin_free" 4 "original" } } +! { dg-final { cleanup-tree-dump "original" } }