Hello! In my opinion the g++ compiler has a problem with for-loop scoping. (Please see attached code snipplet.) The output of the program is:
+++ For Loop +++ Constructor of LoopMe with=98 Destructor of LoopMe with=98 LoopMe op+= called **** I'm invalid and destroyed already **** Constructor of LoopMe with=99 Destructor of LoopMe with=99 LoopMe op+= called **** I'm invalid and destroyed already **** Constructor of LoopMe with=100 Destructor of LoopMe with=100 +++ While Loop +++ Constructor of LoopMe with=98 LoopMe op+= called Destructor of LoopMe with=98 Constructor of LoopMe with=99 LoopMe op+= called Destructor of LoopMe with=99 Constructor of LoopMe with=100 Destructor of LoopMe with=100 IMHO the while loop is correct, while in the for loop, the destructor is called too early. It was also possible to reproduce this on the following architectures: * g++ (GCC) 3.3.2 [Build from original sources] (sparc, Solaris 8) * g++ (GCC) 4.1.0 [Build from original sources] (sparc, Solaris 9) * g++ (GCC) 4.1.0 [Build from original sources] (x86, Redhat9 with kernel 2.6.12.3) * g++ (GCC) 4.2.3 (Debian 4.2.3-2) (x86, Debian unstable with kernel 2.6.24-1-686) * g++ (GCC) 4.2.3 (Debian 4.2.3-1) (x86, Debian unstable with kernel 2.6.23-1-686) * g++-4.3 (Debian 4.3-20080227-1) 4.3.0 20080227 (prerelease) [gcc-4_3-branch revision 132730] (x86, Debian unstable with kernel 2.6.24-1-686) Some searches in this bug database revealed, that Bug#13865 and Bug#16034 look similar, but both are marked as fixed for some years now. If you need additional information, just drop me a mail. Kind regards Andreas Florath -- Summary: for-loop scoping problem: Method gets called after destructor Product: gcc Version: 4.1.0 Status: UNCONFIRMED Severity: minor Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: gnu4u at flonatel dot org GCC build triplet: i486-linux-gnu GCC host triplet: i486-linux-gnu GCC target triplet: i486-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36016