diff mbox

[Fortran] PR 48700: memory leak with MOVE_ALLOC

Message ID BANLkTinjz62g_VDBRURsFBWLrv7cKp7J4g@mail.gmail.com
State New
Headers show

Commit Message

Janus Weil May 16, 2011, 8:37 p.m. UTC
Hi all,

here is a simple patch which takes care of fixing a memory leak, which
occurs when calling MOVE_ALLOC with allocatable scalar arguments. We
simply missed to deallocate the second argument 'TO', which is
INTENT(OUT), in case it is already allocated.

The patch was regtested on x86_64-unknown-linux-gnu. Ok for trunk?

Cheers,
Janus



2011-05-16  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/48700
	* trans-intrinsic.c (gfc_conv_intrinsic_move_alloc): Deallocate 'TO'
	argument to avoid memory leaks.

2011-05-16  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/48700
	* gfortran.dg/move_alloc_4.f90: New.
diff mbox

Patch

Index: gcc/fortran/trans-intrinsic.c
===================================================================
--- gcc/fortran/trans-intrinsic.c	(Revision 173796)
+++ gcc/fortran/trans-intrinsic.c	(Arbeitskopie)
@@ -6958,15 +6958,28 @@  gfc_conv_intrinsic_move_alloc (gfc_code *code)
   if (code->ext.actual->expr->rank == 0)
     {
       /* Scalar arguments: Generate pointer assignments.  */
-      gfc_expr *from, *to;
+      gfc_expr *from, *to, *deal;
       stmtblock_t block;
       tree tmp;
+      gfc_se se;
 
       from = code->ext.actual->expr;
       to = code->ext.actual->next->expr;
 
       gfc_start_block (&block);
 
+      /* Deallocate 'TO' argument.  */
+      gfc_init_se (&se, NULL);
+      se.want_pointer = 1;
+      deal = gfc_copy_expr (to);
+      if (deal->ts.type == BT_CLASS)
+	gfc_add_data_component (deal);
+      gfc_conv_expr (&se, deal);
+      tmp = gfc_deallocate_scalar_with_status (se.expr, NULL, true,
+					       deal, deal->ts);
+      gfc_add_expr_to_block (&block, tmp);
+      gfc_free_expr (deal);
+
       if (to->ts.type == BT_CLASS)
 	tmp = gfc_trans_class_assign (to, from, EXEC_POINTER_ASSIGN);
       else