diff mbox

[Fortran] PR 80121: Memory leak with derived-type intent(out) argument

Message ID CAKwh3qhJYiFbfK9r11kx97kmm=q8R1AAfOfmSp8P+iOd52y=7w@mail.gmail.com
State New
Headers show

Commit Message

Janus Weil April 22, 2017, 9:58 p.m. UTC
Hi all,

the patch in the attachment fixes a memory leak by auto-deallocating
the allocatable components of an allocatable intent(out) argument.

Regtests cleanly on x86_64-linux-gnu. Ok for trunk?

Cheers,
Janus


2017-04-22  Janus Weil  <janus@gcc.gnu.org>

    PR fortran/80121
    * trans-types.c (gfc_conv_procedure_call): Deallocate the components
    of allocatable intent(out) arguments.

2017-04-22  Janus Weil  <janus@gcc.gnu.org>

    PR fortran/80121
    * gfortran.dg/intent_out_9.f90: New test case.

Comments

Thomas Koenig April 22, 2017, 11:32 p.m. UTC | #1
Hi Janus,

> the patch in the attachment fixes a memory leak by auto-deallocating
> the allocatable components of an allocatable intent(out) argument.
>
> Regtests cleanly on x86_64-linux-gnu. Ok for trunk?

OK for trunk.  Also (because this is a quite serious bug)
OK for gcc 7 after the release of 7.1.

Thanks for the patch!

Regards

	Thomas
diff mbox

Patch

Index: gcc/fortran/trans-expr.c
===================================================================
--- gcc/fortran/trans-expr.c	(revision 247077)
+++ gcc/fortran/trans-expr.c	(working copy)
@@ -5454,6 +5454,16 @@  gfc_conv_procedure_call (gfc_se * se, gfc_symbol *
 	      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)))