http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41936
--- Comment #3 from Dominique d'Humieres <dominiq at lps dot ens.fr> --- > Fixed if one adds the code below (copied from gfc_conv_array_parameter). > I'm afraid this could change a memory leak into a double free (see PR 40850). > Also, not quite right (even if the middle-end optimizers are likely to fix > it) > because it adds cleanup code to both foo and bar. With the patch in comment 2, the tests gfortran.dg/alloc_comp_basics_1.f90 and gfortran.dg/alloc_comp_constructor_1.f90 fail because there are 21 occurrences of "builtin_free". This is fixed by the following updated patch (against r209838) --- ../_clean/gcc/fortran/trans-expr.c 2014-04-25 14:30:23.000000000 +0200 +++ gcc/fortran/trans-expr.c 2014-04-27 16:03:28.000000000 +0200 @@ -6474,6 +6474,19 @@ 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->ts.type == BT_CLASS) + && expr->ts.u.derived->attr.alloc_comp && expr->rank + && 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); + } }