public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/13865] New: dtor runs prematurely for loop-scoped variable
@ 2004-01-26  6:26 bryner at brianryner dot com
  2004-01-26  6:29 ` [Bug c++/13865] " bryner at brianryner dot com
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: bryner at brianryner dot com @ 2004-01-26  6:26 UTC (permalink / raw)
  To: gcc-bugs

For a variable declared in the initializer of a for loop, the destructor of the
object runs prior to the first iteration of the loop.  This is incorrect -- the
dtor should run only when exiting the loop's scope.

-- 
           Summary: dtor runs prematurely for loop-scoped variable
           Product: gcc
           Version: tree-ssa
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: bryner at brianryner dot com
                CC: gcc-bugs at gcc dot gnu dot org
  GCC host triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13865


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/13865] dtor runs prematurely for loop-scoped variable
  2004-01-26  6:26 [Bug c++/13865] New: dtor runs prematurely for loop-scoped variable bryner at brianryner dot com
@ 2004-01-26  6:29 ` bryner at brianryner dot com
  2004-01-26  6:36 ` bryner at brianryner dot com
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: bryner at brianryner dot com @ 2004-01-26  6:29 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bryner at brianryner dot com  2004-01-26 06:28 -------
Created an attachment (id=5579)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=5579&action=view)
testcase

$ c++ -v
Reading specs from /usr/gcc-ssa/lib/gcc/i686-pc-linux-gnu/3.5-tree-ssa/specs
Configured with: ../gcc/configure --prefix=/usr/gcc-ssa --enable-shared
--enable-threads=posix --disable-checking --with-system-zlib
--enable-__cxa_atexit --enable-languages=c,c++ --host=i686-pc-linux-gnu
Thread model: posix
gcc version 3.5-tree-ssa 20040125 (merged 20040102)

$ c++ -o test_loop test_loop.cpp
$ ./test_loop
A ctor
A dtor
iteration 0
A ctor
A dtor
iteration 1
A ctor
A dtor

expected output:

A ctor
iteration 0
A ctor
A dtor
iteration 1
A ctor
A dtor
A dtor

No optimization flags are needed in order to reproduce this bug.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13865


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/13865] dtor runs prematurely for loop-scoped variable
  2004-01-26  6:26 [Bug c++/13865] New: dtor runs prematurely for loop-scoped variable bryner at brianryner dot com
  2004-01-26  6:29 ` [Bug c++/13865] " bryner at brianryner dot com
@ 2004-01-26  6:36 ` bryner at brianryner dot com
  2004-01-26 13:10 ` [Bug c++/13865] [tree-ssa] " pinskia at gcc dot gnu dot org
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: bryner at brianryner dot com @ 2004-01-26  6:36 UTC (permalink / raw)
  To: gcc-bugs



-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13865


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/13865] [tree-ssa] dtor runs prematurely for loop-scoped variable
  2004-01-26  6:26 [Bug c++/13865] New: dtor runs prematurely for loop-scoped variable bryner at brianryner dot com
  2004-01-26  6:29 ` [Bug c++/13865] " bryner at brianryner dot com
  2004-01-26  6:36 ` bryner at brianryner dot com
@ 2004-01-26 13:10 ` pinskia at gcc dot gnu dot org
  2004-01-26 14:52 ` bangerth at dealii dot org
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-01-26 13:10 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-01-26 13:09 -------
Confirmed.
The problem is here:

        struct A a;

        __comp_ctor (&a, i);
        try
          {
            
          }
        finally
          {
            __comp_dtor (&a);
          }

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |critical
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|                            |1
   Last reconfirmed|0000-00-00 00:00:00         |2004-01-26 13:09:59
               date|                            |
            Summary|dtor runs prematurely for   |[tree-ssa] dtor runs
                   |loop-scoped variable        |prematurely for loop-scoped
                   |                            |variable
   Target Milestone|---                         |tree-ssa


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13865


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/13865] [tree-ssa] dtor runs prematurely for loop-scoped variable
  2004-01-26  6:26 [Bug c++/13865] New: dtor runs prematurely for loop-scoped variable bryner at brianryner dot com
                   ` (2 preceding siblings ...)
  2004-01-26 13:10 ` [Bug c++/13865] [tree-ssa] " pinskia at gcc dot gnu dot org
@ 2004-01-26 14:52 ` bangerth at dealii dot org
  2004-01-26 23:49 ` bryner at brianryner dot com
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: bangerth at dealii dot org @ 2004-01-26 14:52 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2004-01-26 14:52 -------
Confirmed indeed. And just to make sure that we don't have any copy 
constructors running: 
------------------- 
#include <stdio.h> 
 
struct A { 
    A(int val) : mValue(val) { printf("A ctor\n"); } 
    A(const A&a) : mValue(a.mValue) { printf("A copy ctor\n"); } 
    ~A() { printf("A dtor\n"); } 
 
    int mValue; 
}; 
 
int main() 
{ 
  int i = 0; 
  for (A a = A(i); a.mValue != 2; a = A(++i)) { 
    printf("iteration %d\n", i); 
  } 
} 
------------------------- 
When run this never produces the "A copy ctor" output. 
 
This may well be the cause of some memory corruption people have seen in 
their codes when using tree-ssa. 
 
W. 

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |gerald at pfeifer dot com
           Priority|P2                          |P1
   Last reconfirmed|2004-01-26 13:09:59         |2004-01-26 14:52:16
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13865


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/13865] [tree-ssa] dtor runs prematurely for loop-scoped variable
  2004-01-26  6:26 [Bug c++/13865] New: dtor runs prematurely for loop-scoped variable bryner at brianryner dot com
                   ` (3 preceding siblings ...)
  2004-01-26 14:52 ` bangerth at dealii dot org
@ 2004-01-26 23:49 ` bryner at brianryner dot com
  2004-01-27  0:00 ` pinskia at gcc dot gnu dot org
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: bryner at brianryner dot com @ 2004-01-26 23:49 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bryner at brianryner dot com  2004-01-26 23:49 -------
This testcase is even simpler:

#include <stdio.h>

class A
{
public:
  A() { printf("A ctor\n"); }
  ~A() { printf("A dtor\n"); }
};

int main(int argc, char **argv)
{
  int i = 0;
  for (A a; i < 2; ++i) {
    printf("iteration %d\n", i);
  }

  return 0;
}

which outputs:

A ctor
A dtor
iteration 0
iteration 1

instead of:

A ctor
iteration 0
iteration 1
A dtor

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13865


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/13865] [tree-ssa] dtor runs prematurely for loop-scoped variable
  2004-01-26  6:26 [Bug c++/13865] New: dtor runs prematurely for loop-scoped variable bryner at brianryner dot com
                   ` (4 preceding siblings ...)
  2004-01-26 23:49 ` bryner at brianryner dot com
@ 2004-01-27  0:00 ` pinskia at gcc dot gnu dot org
  2004-01-27 15:41 ` bangerth at dealii dot org
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-01-27  0:00 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-01-27 00:00 -------
Okay this is a problem with the expanders from C++ front-end trees to gimple.

Jason I know that you are the person who did gimplify for the C++ front.
It looks like for's are being expanded wrong.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pinskia at gcc dot gnu dot
                   |                            |org, jason at gcc dot gnu
                   |                            |dot org
      Known to fail|                            |tree-ssa
      Known to work|                            |3.4.0 3.5.0


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13865


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/13865] [tree-ssa] dtor runs prematurely for loop-scoped variable
  2004-01-26  6:26 [Bug c++/13865] New: dtor runs prematurely for loop-scoped variable bryner at brianryner dot com
                   ` (5 preceding siblings ...)
  2004-01-27  0:00 ` pinskia at gcc dot gnu dot org
@ 2004-01-27 15:41 ` bangerth at dealii dot org
  2004-01-29  1:43 ` rth at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: bangerth at dealii dot org @ 2004-01-27 15:41 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2004-01-27 15:41 -------
The example in comment #4 is not a very good one, since the variable 
"a" is only live in the first part of the for-statement. It could thus 
legitimitaly be destroyed even before the first iteration. That wasn't 
the case with the first example, though. 
 
W. 

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13865


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/13865] [tree-ssa] dtor runs prematurely for loop-scoped variable
  2004-01-26  6:26 [Bug c++/13865] New: dtor runs prematurely for loop-scoped variable bryner at brianryner dot com
                   ` (6 preceding siblings ...)
  2004-01-27 15:41 ` bangerth at dealii dot org
@ 2004-01-29  1:43 ` rth at gcc dot gnu dot org
  2004-01-30  4:35 ` cvs-commit at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: rth at gcc dot gnu dot org @ 2004-01-29  1:43 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From rth at gcc dot gnu dot org  2004-01-29 01:43 -------
I can't even see how to begin fixing this.  The precise semantic meaning
of CLEANUP_EXPR eludes me.  Jason, it's all yours.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|unassigned at gcc dot gnu   |jason at gcc dot gnu dot org
                   |dot org                     |
             Status|NEW                         |ASSIGNED


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13865


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/13865] [tree-ssa] dtor runs prematurely for loop-scoped variable
  2004-01-26  6:26 [Bug c++/13865] New: dtor runs prematurely for loop-scoped variable bryner at brianryner dot com
                   ` (8 preceding siblings ...)
  2004-01-30  4:35 ` cvs-commit at gcc dot gnu dot org
@ 2004-01-30  4:35 ` cvs-commit at gcc dot gnu dot org
  2004-01-30 10:56 ` giovannibajo at libero dot it
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu dot org @ 2004-01-30  4:35 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From cvs-commit at gcc dot gnu dot org  2004-01-30 04:34 -------
Subject: Bug 13865

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	tree-ssa-20020619-branch
Changes by:	jason@gcc.gnu.org	2004-01-30 04:34:45

Modified files:
	gcc            : ChangeLog.tree-ssa c-simplify.c 

Log message:
	PR c++/13865
	* c-simplify.c (gimplify_for_stmt): Reorganize to fix cleanups.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.tree-ssa.diff?cvsroot=gcc&only_with_tag=tree-ssa-20020619-branch&r1=1.1.2.1138&r2=1.1.2.1139
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/c-simplify.c.diff?cvsroot=gcc&only_with_tag=tree-ssa-20020619-branch&r1=1.1.4.89&r2=1.1.4.90



-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13865


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/13865] [tree-ssa] dtor runs prematurely for loop-scoped variable
  2004-01-26  6:26 [Bug c++/13865] New: dtor runs prematurely for loop-scoped variable bryner at brianryner dot com
                   ` (7 preceding siblings ...)
  2004-01-29  1:43 ` rth at gcc dot gnu dot org
@ 2004-01-30  4:35 ` cvs-commit at gcc dot gnu dot org
  2004-01-30  4:35 ` cvs-commit at gcc dot gnu dot org
  2004-01-30 10:56 ` giovannibajo at libero dot it
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu dot org @ 2004-01-30  4:35 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From cvs-commit at gcc dot gnu dot org  2004-01-30 04:35 -------
Subject: Bug 13865

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	jason@gcc.gnu.org	2004-01-30 04:35:27

Added files:
	gcc/testsuite/g++.dg/init: for1.C 

Log message:
	PR c++/13865
	* c-simplify.c (gimplify_for_stmt): Reorganize to fix cleanups.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/init/for1.C.diff?cvsroot=gcc&r1=NONE&r2=1.1



-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13865


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/13865] [tree-ssa] dtor runs prematurely for loop-scoped variable
  2004-01-26  6:26 [Bug c++/13865] New: dtor runs prematurely for loop-scoped variable bryner at brianryner dot com
                   ` (9 preceding siblings ...)
  2004-01-30  4:35 ` cvs-commit at gcc dot gnu dot org
@ 2004-01-30 10:56 ` giovannibajo at libero dot it
  10 siblings, 0 replies; 12+ messages in thread
From: giovannibajo at libero dot it @ 2004-01-30 10:56 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From giovannibajo at libero dot it  2004-01-30 10:56 -------
Fixed by Jason's patch:
http://gcc.gnu.org/ml/gcc-patches/2004-01/msg03358.html


-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13865


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2004-01-30 10:56 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-01-26  6:26 [Bug c++/13865] New: dtor runs prematurely for loop-scoped variable bryner at brianryner dot com
2004-01-26  6:29 ` [Bug c++/13865] " bryner at brianryner dot com
2004-01-26  6:36 ` bryner at brianryner dot com
2004-01-26 13:10 ` [Bug c++/13865] [tree-ssa] " pinskia at gcc dot gnu dot org
2004-01-26 14:52 ` bangerth at dealii dot org
2004-01-26 23:49 ` bryner at brianryner dot com
2004-01-27  0:00 ` pinskia at gcc dot gnu dot org
2004-01-27 15:41 ` bangerth at dealii dot org
2004-01-29  1:43 ` rth at gcc dot gnu dot org
2004-01-30  4:35 ` cvs-commit at gcc dot gnu dot org
2004-01-30  4:35 ` cvs-commit at gcc dot gnu dot org
2004-01-30 10:56 ` giovannibajo at libero dot it

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).