473,387 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Exception destruction

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,

--
Bastien
Oct 3 '07 #1
2 1417
On Oct 3, 10:11 am, Bastien Durel <bastien.du...@data.frwrote:
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,

--
Bastien
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

Oct 4 '07 #2
On 04/10/2007 17:05, An**********@gmail.com 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.

--
Bastien
Oct 5 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jacek Dziedzic | last post by:
Hi! I'm trying to make friends with exceptions. I think I'm doing well, there is one thing that bothers me, however. If an object is declared within a try block, it gets destroyed on exception,...
7
by: Douglas Peterson | last post by:
Take a look at this code, it looks funny as its written to be as short as possible: -- code -- struct Base { ~Base() { *((char*)0) = 0; } }; struct Derived : public Base
5
by: Grahamo | last post by:
My question pertains to exceptions and their destruction Say I do this in my (pseudo) code; class an_exception { // details/methods for exception etc. ~an_exception() { .... }; }; try
4
by: Troy | last post by:
We recently installed the .Net framework on a windows 2000 server. Shortly after that we experienced intermitant problems running a web based program that accesses an Access 2002 database. The...
9
by: plahey | last post by:
I have been dabbling in Python for a while now. One of the things that really appeals to me is that I can seem to be able to use C++-style RAII idioms to deal with resource management issues. ...
3
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think...
3
by: benben | last post by:
I have always found Stroustrup's paper on generalized member function wrapper (http://www.research.att.com/~bs/wrapper.pdf) an interesting one. Recently I started to play with it. As I tried to put...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
10
by: Digital Puer | last post by:
Sorry for the rudimentary question. I found the following posting online and did not know the answer myself. "Reminds me of a time I had a phone interview concerning a C++ job. Didn't help...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.