Bug 8287 - GCC3.2: Destructor called for non-constructed local object
Summary: GCC3.2: Destructor called for non-constructed local object
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.2
: P3 normal
Target Milestone: 3.1.x/3.2.x
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2002-10-19 12:16 UTC by kristian.spangsege
Modified: 2004-07-14 03:06 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description kristian.spangsege 2002-10-19 12:16:00 UTC
In the "top-level" scope of a destructor for some class B a local object of class A will have its destructor called even if it was never initialized by its constructor due to a return statement appearing before the declaration of the local object.

The problem does not occur if the local object and the earlier return statement is placed in and ordinary method. Also the problem disappears if the entire destructor body of B is put within an otherwise redundant scope, similarly if only the declaration of the local object is put into a new scope.

The problem also does not occur with GCC-2.96.

The problem obviously results in core dumps unless one of the mentioned work-arounds are applied. But this is close to impossible when porting large applications from earlier versions of gcc.

(I'm utterly amazed about the fact that RedHad has managed to release its RedHat 8.0 entirely base on gcc3.2, when gcc3.2 has susch a lethal bug within it)

Btw. it took me 3 days of reducing 50000 lines of code to around 20 before I was even close to realizing what the bug was.

Go fix it :-)

Release:
gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)

Environment:
RedHat 8.0 (Full install) on dual Intel Pentium III
And various other hardware configs. with RedHat 8.0

How-To-Repeat:
$ g++ test.C
$ ./a.out

And "test.C" is:

#include <iostream>

struct A
{
  A()  { std::cerr << "c\n"; }
  ~A() { std::cerr << "d\n"; }
};

struct B
{
  ~B()
  {
    if(true) return;
    A a;
  }
};

int main()
{
  B b;
  return 0;
}
Comment 1 kristian.spangsege 2002-10-19 12:16:00 UTC
Fix:
http://gcc.gnu.org/ml/gcc-patches/2002-10/msg01817.html
Comment 2 Wolfgang Bangerth 2002-10-28 16:19:29 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: Confirmed with bot 3.2.1pre and CVS. This seems to me like
    a _very_ serious regression, so I raise the priority.
    
    B.t.w.: the problem has nothing to do with copy constructors,
    which usually seem to be the problem in these kind of
    reports.
Comment 3 kristian.spangsege 2002-10-29 01:09:21 UTC
From: Kristian Spangsege <kristian.spangsege@framfab.dk>
To: brian.kristiansen@oracle.com, gcc-gnats@gcc.gnu.org,
	gcc-prs@gcc.gnu.org, kristian.spangsege@framfab.dk,
	gcc-bugs@gcc.gnu.org, nobody@gcc.gnu.org
Cc:  
Subject: Re: c++/8287: GCC3.2: Destructor called for non-constructed local
 object
Date: Tue, 29 Oct 2002 01:09:21 +0100

 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8287
 
Comment 4 Mark Mitchell 2002-10-29 16:07:54 UTC
State-Changed-From-To: analyzed->closed
State-Changed-Why: Fixed in GCC 3.2.1.
Comment 5 Mark Mitchell 2002-10-29 23:51:13 UTC
From: mmitchel@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c++/8287
Date: 29 Oct 2002 23:51:13 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Branch: 	gcc-3_2-branch
 Changes by:	mmitchel@gcc.gnu.org	2002-10-29 15:51:13
 
 Modified files:
 	gcc/cp         : ChangeLog decl.c 
 	gcc/testsuite  : ChangeLog 
 Added files:
 	gcc/testsuite/g++.dg/init: dtor2.C 
 
 Log message:
 	PR c++/8287
 	* decl.c (finish_destructor_body): Create the label to jump to
 	when returning from a destructor here.
 	(finish_function_body): Rather than here.
 	
 	* g++.dg/init/dtor2.C: New test.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.2685.2.114.2.36&r2=1.2685.2.114.2.37
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/decl.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.866.2.36.2.10&r2=1.866.2.36.2.11
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.1672.2.166.2.42&r2=1.1672.2.166.2.43
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/init/dtor2.C.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=NONE&r2=1.1.2.1
 

Comment 6 Mark Mitchell 2002-10-30 00:05:37 UTC
From: mmitchel@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c++/8287
Date: 30 Oct 2002 00:05:37 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Changes by:	mmitchel@gcc.gnu.org	2002-10-29 16:05:36
 
 Modified files:
 	gcc/cp         : ChangeLog decl.c 
 Added files:
 	gcc/testsuite/g++.dg/init: dtor2.C 
 
 Log message:
 	PR c++/8287
 	* decl.c (finish_destructor_body): Create the label to jump to
 	when returning from a destructor here.
 	(finish_function_body): Rather than here.
 	
 	PR c++/8287
 	* g++.dg/init/dtor2.C: New test.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.3030&r2=1.3031
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/decl.c.diff?cvsroot=gcc&r1=1.956&r2=1.957
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/init/dtor2.C.diff?cvsroot=gcc&r1=1.1&r2=1.2
 

Comment 7 Mark Mitchell 2002-10-30 00:05:37 UTC
From: mmitchel@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c++/8287
Date: 30 Oct 2002 00:05:37 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Changes by:	mmitchel@gcc.gnu.org	2002-10-29 16:05:36
 
 Modified files:
 	gcc/cp         : ChangeLog decl.c 
 Added files:
 	gcc/testsuite/g++.dg/init: dtor2.C 
 
 Log message:
 	PR c++/8287
 	* decl.c (finish_destructor_body): Create the label to jump to
 	when returning from a destructor here.
 	(finish_function_body): Rather than here.
 	
 	PR c++/8287
 	* g++.dg/init/dtor2.C: New test.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.3030&r2=1.3031
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/decl.c.diff?cvsroot=gcc&r1=1.956&r2=1.957
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/init/dtor2.C.diff?cvsroot=gcc&r1=1.1&r2=1.2
 

Comment 8 Mark Mitchell 2002-10-30 00:05:57 UTC
From: mmitchel@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c++/8287
Date: 30 Oct 2002 00:05:57 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Changes by:	mmitchel@gcc.gnu.org	2002-10-29 16:05:57
 
 Modified files:
 	gcc/testsuite  : ChangeLog 
 
 Log message:
 	PR c++/8287
 	* g++.dg/init/dtor2.C: New test.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.2190&r2=1.2191