B
Bastien Durel
Hello,
I have a problem with some code using exceptions: I throw an exception
in a method, catch it in the caller, but the instance seems to be
destroyed before the handler can use it ...
here is the caller code :
try
{
LOG("Init parser");
_Init(lOptions, mInitialized);
LOG("Init Done");
}
catch (const TDataParserException & e)
{
LOG("Exception ! ptr: " << (int)(void*)(&e)
<< " - mMessage: " << (int)(void*)e.mMessage);
LOG(e.Where() << ": " << e.What());
mInitialized=false;
mFile.Rollback();
}
and here, the throwing
void USER::Init(const TDataOptions& aOptions, bool& aInitialized)
{
//[...]
if (!(mInputFile.SubStr(0, 3) == "UNA"))
throw TDataParserException("File does not begins with UNA",
__FILE__, __LINE__);
There are my logs, tracing all function calls, and reporting *this
address at the end of the line :
Wed Oct 03 14:27:35 2007: Entering in
TDataParserException::TDataParserException(const char_t*, const char*,
int) (1241792)
Wed Oct 03 14:27:35 2007: TDataParserException: File does not begins
with UNA, ./edifact/user_init.cpp: 48
Wed Oct 03 14:27:35 2007: mMessage is 76738864 mWhere is 76739144
Wed Oct 03 14:27:35 2007: Entering in virtual
TDataParserException::~TDataParserException() (1241792)
Wed Oct 03 14:27:35 2007: Exception ! ptr: 76645864 - mMessage: -1163005939
as e.What() dereferences e.mMessage pointer, it raises an access violation.
The calling code is part of an abstract class, located in a static
library, as _Init() is implemented in the concrete derived class,
located in the main source.
pseudocode :
// in library
class Base {
void Init() {
try { _Init(); }
catch (const TDataParserException& e) { e.What; }
}
virtual void _Init() throw (TDataParserException)=0;
};
// in source
class USER : public Base {
virtual void _Init() throw (TDataParserException) {
throw TDataParserException("");
}
};
I use g++ (GCC) 3.4.2 (mingw-special) to compile the static library, and
the final dll, which is used by another program, making use of of
debugger no easy.
It *looks like* the TDataParserException instance is copyed without use
of the copy constructor (or it would appear in logs), then destroyed.
But this is not coherent.
Have you a tip for me ? An idea about this comportment ?
Thanks,
I have a problem with some code using exceptions: I throw an exception
in a method, catch it in the caller, but the instance seems to be
destroyed before the handler can use it ...
here is the caller code :
try
{
LOG("Init parser");
_Init(lOptions, mInitialized);
LOG("Init Done");
}
catch (const TDataParserException & e)
{
LOG("Exception ! ptr: " << (int)(void*)(&e)
<< " - mMessage: " << (int)(void*)e.mMessage);
LOG(e.Where() << ": " << e.What());
mInitialized=false;
mFile.Rollback();
}
and here, the throwing
void USER::Init(const TDataOptions& aOptions, bool& aInitialized)
{
//[...]
if (!(mInputFile.SubStr(0, 3) == "UNA"))
throw TDataParserException("File does not begins with UNA",
__FILE__, __LINE__);
There are my logs, tracing all function calls, and reporting *this
address at the end of the line :
Wed Oct 03 14:27:35 2007: Entering in
TDataParserException::TDataParserException(const char_t*, const char*,
int) (1241792)
Wed Oct 03 14:27:35 2007: TDataParserException: File does not begins
with UNA, ./edifact/user_init.cpp: 48
Wed Oct 03 14:27:35 2007: mMessage is 76738864 mWhere is 76739144
Wed Oct 03 14:27:35 2007: Entering in virtual
TDataParserException::~TDataParserException() (1241792)
Wed Oct 03 14:27:35 2007: Exception ! ptr: 76645864 - mMessage: -1163005939
as e.What() dereferences e.mMessage pointer, it raises an access violation.
The calling code is part of an abstract class, located in a static
library, as _Init() is implemented in the concrete derived class,
located in the main source.
pseudocode :
// in library
class Base {
void Init() {
try { _Init(); }
catch (const TDataParserException& e) { e.What; }
}
virtual void _Init() throw (TDataParserException)=0;
};
// in source
class USER : public Base {
virtual void _Init() throw (TDataParserException) {
throw TDataParserException("");
}
};
I use g++ (GCC) 3.4.2 (mingw-special) to compile the static library, and
the final dll, which is used by another program, making use of of
debugger no easy.
It *looks like* the TDataParserException instance is copyed without use
of the copy constructor (or it would appear in logs), then destroyed.
But this is not coherent.
Have you a tip for me ? An idea about this comportment ?
Thanks,