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: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;
}
}
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: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;
}
}