By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,538 Members | 950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,538 IT Pros & Developers. It's quick & easy.

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

P: n/a
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::ostringstream 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;
}
}

Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ro****@howzatt.demon.co.uk wrote:
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
Nov 17 '05 #2

P: n/a
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.
--
MVP in C++ at www.brainbench.com

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.