From patchwork Thu Oct 14 14:21:37 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Janus Weil X-Patchwork-Id: 67830 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id C127AB70ED for ; Fri, 15 Oct 2010 01:21:54 +1100 (EST) Received: (qmail 28450 invoked by alias); 14 Oct 2010 14:21:48 -0000 Received: (qmail 28432 invoked by uid 22791); 14 Oct 2010 14:21:46 -0000 X-SWARE-Spam-Status: No, hits=-0.1 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE X-Spam-Check-By: sourceware.org Received: from mail-bw0-f47.google.com (HELO mail-bw0-f47.google.com) (209.85.214.47) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 14 Oct 2010 14:21:40 +0000 Received: by bwz9 with SMTP id 9so501187bwz.20 for ; Thu, 14 Oct 2010 07:21:38 -0700 (PDT) MIME-Version: 1.0 Received: by 10.204.63.199 with SMTP id c7mr9281324bki.110.1287066097221; Thu, 14 Oct 2010 07:21:37 -0700 (PDT) Received: by 10.204.136.203 with HTTP; Thu, 14 Oct 2010 07:21:37 -0700 (PDT) In-Reply-To: <4CB5AE74.4080602@net-b.de> References: <4CB4C678.5050008@net-b.de> <4CB5AE74.4080602@net-b.de> Date: Thu, 14 Oct 2010 16:21:37 +0200 Message-ID: Subject: Re: [Patch, Fortran] PR 42647: Missed initialization/dealloc of allocatable scalar DT with allocatable component From: Janus Weil To: Tobias Burnus Cc: gfortran , gcc-patches Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org 2010/10/13 Tobias Burnus : >  On 10/13/2010 02:08 PM, Janus Weil wrote: >> >> Self-review: Three things were still missing: >> 1) Auto-deallocation of CLASS components. >> 2) Auto-deallocation of "sub-components", i.e. stuff like a%b%c. >> 3) Test cases. > > I have not yet had a closer look at the patch, but I think you should also > take care of the DEALLOCATE statement. Example: > > type t >  integer, allocatable :: p > end type t > type(t), allocatable :: a > > deallocate(a) > end > > (Allocate left away to make dump cleaner.) The DEALLOCATE is translated into > the following. You see many accesses to "a.p" which cause a segfault if "a" > is not allocated - assume > >      if (a.p != 0B) >        { >          __builtin_free ((void *) a.p); >        } >      a.p = 0B; >      if (a == 0B)         { >          _gfortran_runtime_error_at >        }       else         { >          __builtin_free ((void *) a); >        } >      a = 0B; > > Hmm, I actually could not make the program crash - even though the dump > looks wrong. Yes, you're right. I need to restructure my patch a bit to make it work also for DEALLOCATE. > In any case the following program leaks memory (without the "if" line); with > the "if" line it aborts as "istat" is not zero, without stat= it aborts with > an error message stating that "a" is not allocated. Expected: No error, no > valgrind error and istat == 0. > > type t >  integer, allocatable :: p(:) > end type t > type(t), allocatable :: a > > allocate(a) > allocate(a%p(10000)) > > deallocate(a, stat=istat) > if(istat /= 0) call abort() > end This is fixed by the following hunk: Cheers, Janus Index: gcc/fortran/trans-stmt.c =================================================================== --- gcc/fortran/trans-stmt.c (revision 165461) +++ gcc/fortran/trans-stmt.c (working copy) @@ -4679,7 +4679,11 @@ if (!(last && last->u.c.component->attr.pointer) && !(!last && expr->symtree->n.sym->attr.pointer)) { - tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, se.expr, + if (!expr->rank) + tmp = build_fold_indirect_ref_loc (input_location, se.expr); + else + tmp = se.expr; + tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, tmp, expr->rank); gfc_add_expr_to_block (&se.pre, tmp); }