473,789 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ exceptions and destructors

Hi!

I've got a question concerning C++ exceptions, more precisely the
stack unwinding when an exception occurs. We agree that when an
exception is thrown, all objects that have destructors are
"destructed " by calling the corresponding destructor. This is most
often what is wanted. However,I have a special situation where this is
NOT what I'd like to happen : indeed, i'd like to keep my objects
living on the stack. Practically, this would be to implement a
function tracing method that would be able, when my program crashes
(exception !), to tell me in which function it happened, just like
unreal.
Is there a way of telling the compiler that an object should not be
destroyed upon exception ? Or, is there a way to know that the
destructor gets called because of an exception and not because the
function exited "normally" ?

Thanks in advance !
Magalie
Jul 19 '05 #1
9 3322
Magalie escribió:
Is there a way of telling the compiler that an object should not be
destroyed upon exception ? Or, is there a way to know that the
destructor gets called because of an exception and not because the
function exited "normally" ?


You can allocate the object with new and delete it just before the
function exits... and take care when you add a new "return" to the
function.

Alternatively, you can have an object of a handle class that in his
destructor checks std::uncaught_e xception () and destroys or not the
object handled in each case.

In any case you probably need also a way to store pointers to the
allocated and not deleted objects.

Regards.
Jul 19 '05 #2
Hi,
"Magalie" <do**********@l aposte.net> wrote in message
news:3f******** *************** ***@posting.goo gle.com...
[....all ok....]
Practically, this would be to implement a
function tracing method that would be able, when my program crashes
(exception !), to tell me in which function it happened, just like
unreal.
Is there a way of telling the compiler that an object should not be
destroyed upon exception ? No, except by allocating it on the heap and manually destroying it
upon function exit. Or, is there a way to know that the
destructor gets called because of an exception and not because the
function exited "normally" ?

Well, there is a function called uncaught_except ion() which
returns a boolean. Unfortunately, it cannot be used reliably:
http://www.gotw.ca/gotw/047.htm

So regarding what you are trying to implement, here's what I
would suggest:
If you are ok with using platform-specific features,
some compilers do provide a way to access information
about enclosing stack frames in debug mode. Please ask
in a forum dedicated to your platform.
Alternatively, information about the enclosing calls
should be made available to the exception-throwing code.

Here's a quick overview of what I would try:

class ScopeInfo {
private:
static ScopeInfo* head; // or use TLS for threaded app
//NB: don't forget define&init in a cpp file...
ScopeInfo* const next;
char const* const info;
ScopeInfo(Scope Info const&); // not-implemented
public:
ScopeInfo(char const* info_)
: next(head), info(info_) { head=this; };
~ScopeInfo() { head=next; }

static print() {
std::cerr << " In scope:\n";
for( ScopeInfo* p = head ; p ; p=p->next )
std::cerr << p->info << '\n';
}
};

- ScopeInfo are instantiated on the stack as desired
- on throw location, call ScopeInfo::prin t (or store
the info within the exception)

That's just an option I can think of -- I'm curions
if there are any other ideas...
Cheers,
Ivan
--
http://www.post1.com/~ivec
Jul 19 '05 #3

"Magalie" <do**********@l aposte.net> wrote in message news:3f******** *************** ***@posting.goo gle.com...>
. However,I have a special situation where this is
NOT what I'd like to happen : indeed, i'd like to keep my objects
living on the stack.
There is no way to do this. The stack containing these objects
ceases to exist once the catch block is reached.
Practically, this would be to implement a
function tracing method that would be able, when my program crashes
(exception !), to tell me in which function it happened, just like
unreal.
So why don't you put special code in the destructors to record this information?
Is there a way of telling the compiler that an object should not be
destroyed upon exception ?
No.
Or, is there a way to know that the
destructor gets called because of an exception and not because the
function exited "normally" ?


One scheme that I have used to implement a similar feature is to use
a special exception object that I throw, that when it is constructed sets
a static bool that says I'm handling exceptions, then the TraceBack objects
check that in their destructor and if so log their destruction.

A few macros that initalizie the TraceBack object with __FILE__ and __LINE__
give more information on the trace.
Jul 19 '05 #4

Magalie wrote:

Hi!

I've got a question concerning C++ exceptions, more precisely the
stack unwinding when an exception occurs. We agree that when an
exception is thrown, all objects that have destructors are
"destructed " by calling the corresponding destructor.
"We" don't agree. This only makes sense if there's a handler (in
the dynamic context) to catch AND HANDLE your exception -- you'd
really want to fully unwind the stack then (and only then).
This is most
often what is wanted. However,I have a special situation where this is
NOT what I'd like to happen : indeed, i'd like to keep my objects
living on the stack.
Uncaught exception shall invoke unexpected() without any stack
unwinding. The only problem is that unexpected() is {currently}
invoked only on ES violation, AFTER silly unwinding, and with
totally messed up terminate() and unexpected() handlers.

http://groups.google.com/groups?selm...16EC0%40web.de
(Subject: Re: A few exception questions)
Practically, this would be to implement a
function tracing method that would be able, when my program crashes
(exception !), to tell me in which function it happened, just like
unreal.


Do you know what "core dumped" mean? Or are you on a Sutter's
"platform"? ``Hear hear.''

http://search.microsoft.com/search/r...px?qu=Userdump

regards,
alexander.
Jul 19 '05 #5

"Magalie" <do**********@l aposte.net> wrote in message
news:3f******** *************** ***@posting.goo gle.com...
Hi!

I've got a question concerning C++ exceptions, more precisely the
stack unwinding when an exception occurs. We agree that when an
exception is thrown, all objects that have destructors are
"destructed " by calling the corresponding destructor. This is most
often what is wanted. However,I have a special situation where this is
NOT what I'd like to happen : indeed, i'd like to keep my objects
living on the stack. Practically, this would be to implement a
function tracing method that would be able, when my program crashes
(exception !), to tell me in which function it happened, just like
unreal.
Is there a way of telling the compiler that an object should not be
destroyed upon exception ? Or, is there a way to know that the
destructor gets called because of an exception and not because the
function exited "normally" ?


I think the normal way to do that would be to add that information to a
parameter passed on the exception itself. For example

void functionA()
{
throw "Error in functionA";
}

Of course, you can get more sophisticated and throw exceptions of your own
classes, with more information inside them.
Jul 19 '05 #6
> We agree that when an
exception is thrown, all objects that have destructors are
"destructed " by calling the corresponding destructor.
No, this is not what happens. Destructors are called only when
objects go out of scope, or delete is called on object created with
new. Consider the following..

#include <iostream>

class foo
{
std::string s_;
public:
foo(std::string s):s_(s) {}
~foo() { std::cout << s_ << "'s ~foo()"<< std::endl;}
};

int main(int argc , char * argv[])
{
try
{
foo * f ( new foo("f") );
foo x("x");
throw 1;
}
catch(int e)
{
std::cout << "in catch, e=" << e << std::endl;
}
}
When executed, it prints...

x's ~foo()
in catch, e=1

The destructor for the object pointed to by f doesn't get called
because it never goes out of scope. That's why they implemented
auto_ptr. Its used like this...

#include <iostream>
#include <memory>

class foo
{
std::string s_;
public:
foo(std::string s):s_(s) {}
~foo() { std::cout << s_ << "'s ~foo()"<< std::endl;}
};

int main(int argc , char * argv[])
{
try
{
std::auto_ptr<f oo> f( new foo("f") );
foo x("x");
throw 1;
}
catch(int e)
{
std::cout << "in catch, e=" << e << std::endl;
}
}
When run, it prints the following...
x's ~foo()
f's ~foo()
in catch, e=1

This is most
often what is wanted. However,I have a special situation where this is
NOT what I'd like to happen : indeed, i'd like to keep my objects
living on the stack.
This can only happen if those object don't go out of scope. Like
this...

#include <iostream>
#include <memory>

class foo
{
std::string s_;
public:
foo(std::string s):s_(s) {}
~foo() { std::cout << s_ << "'s ~foo()"<< std::endl;}
};

int main(int argc , char * argv[])
{
std::auto_ptr<f oo> f( new foo("f") );
foo x("x");

try
{
throw 1;
}
catch(int e)
{
std::cout << "in catch, e=" << e << std::endl;
}
}
This prints the following

in catch, e=1
x's ~foo()
f's ~foo()

Thus, the object x and f are still on the stack when the exception is
caught.
Practically, this would be to implement a
function tracing method that would be able, when my program crashes
(exception !), to tell me in which function it happened, just like
unreal.
If you load the core file produced when your program crashes into a
debugger, you will get the same information.
Is there a way of telling the compiler that an object should not be
destroyed upon exception ?


The compiler doesn't destroy anything just because of an exception
being thrown, in fact you need to make sure that you destroy things
that need to be destroyed yourself, that's why auto_ptr is so nice.
Basically, the compiler only destroyes objects which go out of scope.
Jul 19 '05 #7

"Big Brian" <wo**@brianmiel ke.com> wrote in message
news:d2******** *************** **@posting.goog le.com...
We agree that when an
exception is thrown, all objects that have destructors are
"destructed " by calling the corresponding destructor.


No, this is not what happens. Destructors are called only when
objects go out of scope, or delete is called on object created with
new.


But that is exactly what happens as the stack unwinds. Surely, the original
poster did not literally mean "all objects" - he/she meant all objects
"below" the catch in scope. I doubt anyone thinks all objects at the same
scope as the exception handler are also wiped out.
Jul 19 '05 #8

"jeffc" <no****@nowhere .com> wrote in message news:3f******** @news1.prserv.n et...

But that is exactly what happens as the stack unwinds. Surely, the original
poster did not literally mean "all objects" - he/she meant all objects
"below" the catch in scope. I doubt anyone thinks all objects at the same
scope as the exception handler are also wiped out.


Well actually, everything in the try block and things descended from it. It certainly
does not destroy things in the scope of the exception handler (they ahven't been
created yet).

SomeClass A;
try {
SomeClass B;
function_call() ;
} catch(...) {
SomeClass C;
}

WHen the catch begins, A is still valid, B and everyting in function_call and beyond
is destroyed, C has not yet been created.

Jul 19 '05 #9

"Ron Natalie" <ro*@sensor.com > wrote in message
news:3f******** *************** @news.newshosti ng.com...

"jeffc" <no****@nowhere .com> wrote in message news:3f******** @news1.prserv.n et...

But that is exactly what happens as the stack unwinds. Surely, the original poster did not literally mean "all objects" - he/she meant all objects
"below" the catch in scope. I doubt anyone thinks all objects at the same scope as the exception handler are also wiped out.
Well actually, everything in the try block and things descended from it.

It certainly does not destroy things in the scope of the exception handler (they ahven't been created yet).


Right, I should have said that in terms of the try block, not the catch.
Jul 19 '05 #10

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

Similar topics

16
5294
by: David Turner | last post by:
Hi all I noticed something interesting while testing some RAII concepts ported from C++ in Python. I haven't managed to find any information about it on the web, hence this post. The problem is that when an exception is raised, the destruction of locals appears to be deferred to program exit. Am I missing something? Is this behaviour by design? If so, is there any reason for it? The only rationale I can think of is to speed up...
7
9251
by: Michael Andersson | last post by:
Hi! Does the use of exception handling induce a performance penalty during the execution of non exception handling code? Regards, /Michael
12
533
by: Siemel Naran | last post by:
(1) About std::exit. If I call this function, will the system call the destructors of all objects in the call stack. Eg. void f() { std::exit(1); } void g() { X x; f(); } Does the call in f() to std::exit invoke x.~X() in scope g()? (2) About signals and exceptions. Is it possible to throw an exception from
21
4444
by: mihai | last post by:
People say that is a bad technique to throw exception from constructors; and that the solution would be to create a function _create_ to initialize an object. What about copy constructors? How can we avoid throwing exceptions? If we already have an abject witch was initialized wit _create_ we will be forced to call create in copy constructor and to throw exceptions from it. Have a nice day,
59
4453
by: kk_oop | last post by:
Hi. I wanted to use exceptions to handle error conditions in my code. I think doing that is useful, as it helps to separate "go" paths from error paths. However, a coding guideline has been presented that says "Use conventional error-handling techniques rather than exception handling for straightforward local error processing in which a program is easily able to deal with its own errors." By "conventional error-handling," I believe...
15
426
by: Bernard | last post by:
Hi All, I am not sure if I should be asking this question on clc or clc++. Let me try on both. I hope that this is not too trivial for the brilliant minds over here. I know that OOP questions have been asked on clc before so it is probably OK. I am a newbie to C++. BS 3rd edition states: % The throw transfers control to a handler for exceptions .... %
40
3161
by: Mark P | last post by:
I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost fcn is a boolean and there are certain places within the inner functions where it may become apparent that the return value is false. In this case I'd like to halt the computation immediately and return false. My current approach is to have...
42
3050
by: Jon Harrop | last post by:
Why are exceptions in C++ ~6x slower than exceptions in OCaml? -- Dr Jon D Harrop, Flying Frog Consultancy Objective CAML for Scientists http://www.ffconsultancy.com/products/ocaml_for_scientists/index.html?usenet
5
2086
by: Rennie deGraaf | last post by:
I know that if an exception is thrown from a destructor while unwinding the stack because of another exception, terminate() is called (FAQ 17.3). How exactly does this rule work? Is it acceptable to both throw /and/ catch an exception inside a destructor, as in the following code? struct Foo { void finalize() { throw 1.0;
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3700
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.