This patch fixes a memory leakage with allocatables and user-defined operators. It is basically Mikael's patch adjusted in order to take into account Tobias' comments. The patch fixes the memory leak in the test gfortran.dg/class_array_15.f03 and I have added a check for it.
In the PR I asked the question if this sufficient or if I should also include the test in comment 0. Any opinion? Cheers, Dominique 2014-05-27 Dominique d'Humieres <domi...@lps.ens.fr> Mikael Morin <mik...@gcc.gnu.org> PR fortran/41936 * trans-expr.c (gfc_conv_expr_reference): Deallocate array components. 2014-05-27 Dominique d'Humieres <domi...@lps.ens.fr> Mikael Morin <mik...@gcc.gnu.org> PR fortran/41936 * gfortran.dg/class_array_15.f03: Check memory leaks. diff -up ../_clean/gcc/fortran/trans-expr.c gcc/fortran/trans-expr.c --- ../_clean/gcc/fortran/trans-expr.c 2014-05-22 10:05:53.000000000 +0200 +++ gcc/fortran/trans-expr.c 2014-05-25 00:50:21.000000000 +0200 @@ -6506,6 +6506,20 @@ gfc_conv_expr_reference (gfc_se * se, gf /* Take the address of that value. */ se->expr = gfc_build_addr_expr (NULL_TREE, var); + if (expr->ts.type == BT_DERIVED && expr->rank + && !gfc_is_finalizable (expr->ts.u.derived, NULL) + && expr->ts.u.derived->attr.alloc_comp + && expr->expr_type != EXPR_VARIABLE) + { + tree tmp; + + tmp = build_fold_indirect_ref_loc (input_location, se->expr); + tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, tmp, expr->rank); + + /* The components shall be deallocated before + their containing entity. */ + gfc_prepend_expr_to_block (&se->post, tmp); + } } diff -up ../_clean/gcc/testsuite/gfortran.dg/class_array_15.f03 gcc/testsuite/gfortran.dg/class_array_15.f03 --- ../_clean/gcc/testsuite/gfortran.dg/class_array_15.f03 2013-01-06 22:34:50.000000000 +0100 +++ gcc/testsuite/gfortran.dg/class_array_15.f03 2014-05-04 10:24:06.000000000 +0200 @@ -1,4 +1,5 @@ ! { dg-do run } +! { dg-options "-fdump-tree-original" } ! ! Tests the fixes for three bugs with the same underlying cause. All are regressions ! that come about because class array elements end up with a different tree type @@ -114,3 +115,5 @@ subroutine pr54992 ! This test remains bh => bhGet(b,instance=2) if (loc (b) .ne. loc(bh%hostNode)) call abort end +! { dg-final { scan-tree-dump-times "builtin_free" 12 "original" } } +! { dg-final { cleanup-tree-dump "original" } }