bad destructor call in exception unwind with VC.NET 2003.

R

rogero

I'm having a nasty problem where the destructor of an automatic
variable is invoked with the wrong address during stack unwinding.
Below is a small example program.

Roger Orr
--
MVP in C++ at www.brainbench.com

------------------------------------------------------------------
/*
NAME
badDtor.cpp

DESCRIPTION
Demonstrate a bad call to a destructor during stack unwinding.

When an exception is thrown inside 'testFunction' the call to
destruct the local variable 'tester' passes the _wrong address_
to the destructor.

ENVIRONMENT
Visual Studio .NET 2003
Windows XP Professional 2002 SP 1

Compiled as:
cl /O2 /EHsc /MD /GS /W3 /Zi badDtor.cpp

(Code works without fault when using:
/Od or /O1 rather than /O2
/Oy- as well as /O2)

ACTUAL OUTPUT
ctor: 0012FF28
testFunction called
FAILED !!
dtor: 0012FFA0 value: 3291616, expected: 12345678
Caught: 0

EXPECTED OUTPUT
ctor: 0012FF28
testFunction called
dtor: 0012FF28
Caught: 0
*/

#include <iostream>
#include <sstream>
#include <string>

#include <malloc.h>

// Simple class which logs construction and destruction.
class Tester
{
private:
int value;
static const int cookie = 12345678;
public:
Tester()
: value( cookie )
{
std::cout << "ctor: " << this << std::endl;
}
~Tester()
{
if ( value == cookie )
std::cout << "dtor: " << this << std::endl;
else
std::cout << "FAILED !!\ndtor: " << this << " value: " <<
value << ", expected: " << cookie << std::endl;
}
};

// Test function that throws and calls destructor of 'tester'
incorrectly during unwind.
void testFunction()
{
Tester tester;

{
std::blush:stringstream oss;
oss << "testFunction called";
std::cout << oss.str() << std::endl;
}

void *p = _alloca( 100 );

throw 0;
}

int main()
{
try
{
testFunction();
}
catch ( int & ex )
{
std::cerr << "Caught: " << ex << std::endl;
}
catch ( ... )
{
std::cerr << "Unexpected exception!" << std::endl;
}
}
 
C

Carl Daniel [VC++ MVP]

I'm having a nasty problem where the destructor of an automatic
variable is invoked with the wrong address during stack unwinding.
Below is a small example program.

Thanks for posting a thorough repro case!

This bug appears to be fixed in current builds of VC8 (I tested build
50725 - it may be fixed in earlier builds as well). If this bug is causing
you severe problems with VC7.1, you should contact product support to see if
a hotfix is available, or to request one.

In this case, unless you really need -O2 (and you know you need it based on
profiling), I'd recomend simply compiling with -O1 instead. It's not
uncommon that -O1 actually produces faster code than -O2, and using it
avoids a fair number of optimization bugs.

-cd
 
R

rogero

Thanks for the rapid reply.

I will try changing the settings for log4cxx 0.9.7 (which is the
original component exhibiting the fault).

Regards,
Roger Orr.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Optimization issue with VC7.1 1
bug in VC++2003 1

Top