Exception destruction

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,
 
A

AnonMail2005

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 suspect your exception class has issues but you need to put the code
for the class here so we can see it.

To prove that it's the exception class, and not the throwing or
catching
of it, just throw and catch an exception from std (e.g.
std::exception).
If your code works, then you can reasonably assume that the
implementation
of your specific exception class is at issue.

hth
 
B

Bastien Durel

On 04/10/2007 17:05, (e-mail address removed) wrote:
[...]
I suspect your exception class has issues but you need to put the code
for the class here so we can see it.

To prove that it's the exception class, and not the throwing or
catching
of it, just throw and catch an exception from std (e.g.
std::exception).
If your code works, then you can reasonably assume that the
implementation
of your specific exception class is at issue.

hth

Hello,

It was a mistake in the exception constructor, you're right.
 

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

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top