From 60fc41bd415a631d7db82d56ed44d95677edf57e Mon Sep 17 00:00:00 2001 From: Janus Weil Date: Sun, 23 Apr 2017 10:26:50 +0200 Subject: [PATCH] re PR fortran/80121 (Memory leak with derived-type intent(out) argument) 2017-04-22 Janus Weil PR fortran/80121 * trans-types.c (gfc_conv_procedure_call): Deallocate the components of allocatable intent(out) arguments. 2017-04-22 Janus Weil PR fortran/80121 * gfortran.dg/intent_out_9.f90: New test case. From-SVN: r247083 --- gcc/fortran/ChangeLog | 6 +++++ gcc/fortran/trans-expr.c | 10 ++++++++ gcc/testsuite/ChangeLog | 5 ++++ gcc/testsuite/gfortran.dg/intent_out_9.f90 | 28 ++++++++++++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 gcc/testsuite/gfortran.dg/intent_out_9.f90 diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index 7058511d565..b186bfd65aa 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,9 @@ +2017-04-22 Janus Weil + + PR fortran/80121 + * trans-types.c (gfc_conv_procedure_call): Deallocate the components + of allocatable intent(out) arguments. + 2017-04-21 Janus Weil PR fortran/80392 diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c index 7bced25df49..af1549a611f 100644 --- a/gcc/fortran/trans-expr.c +++ b/gcc/fortran/trans-expr.c @@ -5454,6 +5454,16 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym, if (fsym && fsym->attr.allocatable && fsym->attr.intent == INTENT_OUT) { + if (fsym->ts.type == BT_DERIVED + && fsym->ts.u.derived->attr.alloc_comp) + { + // deallocate the components first + tmp = gfc_deallocate_alloc_comp (fsym->ts.u.derived, + parmse.expr, e->rank); + if (tmp != NULL_TREE) + gfc_add_expr_to_block (&se->pre, tmp); + } + tmp = build_fold_indirect_ref_loc (input_location, parmse.expr); if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp))) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 39c270773f2..27f5be805ec 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2017-04-22 Janus Weil + + PR fortran/80121 + * gfortran.dg/intent_out_9.f90: New test case. + 2017-04-23 Uros Bizjak PR target/70799 diff --git a/gcc/testsuite/gfortran.dg/intent_out_9.f90 b/gcc/testsuite/gfortran.dg/intent_out_9.f90 new file mode 100644 index 00000000000..e2536927210 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/intent_out_9.f90 @@ -0,0 +1,28 @@ +! { dg-do compile } +! { dg-options "-fdump-tree-original" } +! +! PR 80121: Memory leak with derived-type intent(out) argument +! +! Contributed by Andrew Wood + +PROGRAM p + IMPLICIT NONE + TYPE t1 + INTEGER, ALLOCATABLE :: i(:) + END TYPE + call leak + CONTAINS + SUBROUTINE s1(e) + TYPE(t1), ALLOCATABLE, INTENT(OUT) :: e(:) + ALLOCATE( e(1) ) + ALLOCATE( e(1)%i(2) ) + END SUBROUTINE + SUBROUTINE leak + TYPE(t1), ALLOCATABLE :: e(:) + CALL s1(e) + CALL s1(e) + END SUBROUTINE +END PROGRAM + +! { dg-final { scan-tree-dump-times "__builtin_free" 6 "original" } } +! { dg-final { cleanup-tree-dump "original" } } -- 2.30.2