Exception Handling Question and Unwinding stack

D

DanielJohnson

I have a little confusion with this kind of situation.

Suppose there are two functions, Function First allocates a memory and
then call Function Second..... But in Function Second Exception
occurs!!! How can we notify Function First about this in order to
deallocate the memory allocated by First ?

I found something from Stroutup's web page, saying that it can't be
done ?

http://www.research.att.com/~bs/bs_faq2.html#resume

I was thinking that if we can catch this exception in the second
function then either 1) rethrow so that the first function can catch
it then deallocate appropriately 2) Catch and return an error value
OR
The second is that a function doesn't have to catch it at all. The
exception would automatically be rethrow to the first function.


Can C++ gurus throw some insight into such situations ?
 
A

Alf P. Steinbach

* DanielJohnson:
I have a little confusion with this kind of situation.

Suppose there are two functions, Function First allocates a memory and
then call Function Second..... But in Function Second Exception
occurs!!! How can we notify Function First about this in order to
deallocate the memory allocated by First ?

I found something from Stroutup's web page, saying that it can't be
done ?

http://www.research.att.com/~bs/bs_faq2.html#resume

I was thinking that if we can catch this exception in the second
function then either 1) rethrow so that the first function can catch
it then deallocate appropriately 2) Catch and return an error value
OR
The second is that a function doesn't have to catch it at all. The
exception would automatically be rethrow to the first function.


Can C++ gurus throw some insight into such situations ?

It seems you have misunderstood Bjarne's comment, which was about
resumption.

Ordinary C++ exception handling suffices for the situation you describe.

In particular,

void foo()
{
std::vector<int> v(256);
bar();
tender();
}

allocates memory dynamically for v and, if that succeeds, is guaranteed
to deallocate that memory if bar() or tender() throws.

Resumption, to continue execution at the point of the throw, is fraught
with dangers. However, in some cases you may want a function or block
to retry, perhaps in some other way, if an exception occurs. The basic
mechanism is then a loop, and that retry loop may be abstracted by using
the template pattern (which has nothing to do with C++ templates).


Cheers, & hth.,

- Alf
 
E

Erik Wikström

I have a little confusion with this kind of situation.

Suppose there are two functions, Function First allocates a memory and
then call Function Second..... But in Function Second Exception
occurs!!! How can we notify Function First about this in order to
deallocate the memory allocated by First ?

I found something from Stroutup's web page, saying that it can't be
done ?

http://www.research.att.com/~bs/bs_faq2.html#resume

I was thinking that if we can catch this exception in the second
function then either 1) rethrow so that the first function can catch
it then deallocate appropriately 2) Catch and return an error value
OR
The second is that a function doesn't have to catch it at all. The
exception would automatically be rethrow to the first function.

You are misinterpreting the link. There are three ways to notify First
that an exception occurred: 1) you can just let the exception propagate
and catch it in First, 2) you can catch the exception in Second and
throw a new/rethrow, or 3) you can catch in Second and return with some
error code.

What the link says is that you can not in either First or Second perform
some kind of error recovery and then re-run the code that threw the
exception (though you can of course call Second again from First,
perhaps in a loop)
 
M

Martin York

How can we notify Function First about this in order to
deallocate the memory allocated by First ?


C++ handles resource management via the constructor/destructor
mechanism. Just make sure all memory allocation/deallocation is done
inside an objects construtor/destructor. Then create the object on the
stack. When the stack unwinds the destructor will auto-magically de-
allocate the resource.

std::vector<> is great if you want to allocate a bunch of objects
dynamically.
std::auto_ptr<> is find to allocate one object.

But if you must do it yourself then somthing like this:

class MyMemoryForX
{
MyMemoryForX(int size)
{
data = new X[size];
}
~MyMemoryForX()
{
delete [] data;
}
private:
X* data;

MyMemoryForX(MyMemoryForX const&); // don't define
void operator=(MyMemoryForX const&); // don't define
};

void first
{
MyMemoryForX x(200);
second(); // Any exception thrown in here will cause
// the stack to un-wind and the destructor
// for x to be called. Thus resulting in
// resource clean up.
}
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top