is such exception handling approach good?

G

George2

Hello everyone,


Suppose I have some objects created on local function stack (not on
heap). And I allocate and free all the related resources in the
constructor and destructor (e.g. memory and file handles). And I do
not implement explicit exception handling code in the function for
resource free purpose, and simply rely on destructor to free resources
if exception occurs, that is,

1. when exception occurs and the exception triggers us to go out of
current function stack to the caller to find exception handlers;
2. since objects are allocated on local stack, when exception triggers
us to go out of current stack to its caller to find exception handler,
the lifecycle of local objects on local stack will expire, and its
related destructor will be invoked and free resources.

Pseudo code like this,

Code:
void func()
{
      Class1 inst1;
      Class2 inst2;

      ... // some operations

      return;
}

Does above code always safe? Are there any potential risks to leak
resources?


thanks in advance,
George
 
R

Rolf Magnus

George2 said:
Hello everyone,


Suppose I have some objects created on local function stack (not on
heap).

The C++ standard does define the words "heap" and "stack", but both mean
something different from what you are talking about.
Local variables go to automatic storage. Dynamic objects go to the free
storage.
1. when exception occurs and the exception triggers us to go out of
current function stack to the caller to find exception handlers;
2. since objects are allocated on local stack, when exception triggers
us to go out of current stack to its caller to find exception handler,
the lifecycle of local objects on local stack will expire, and its
related destructor will be invoked and free resources.
Yes.


Pseudo code like this,

Code:
void func()
{
Class1 inst1;
Class2 inst2;

... // some operations

return;
}

Does above code always safe? Are there any potential risks to leak
resources?

If Class1 and Class2 have proper destructors that release all acquired
resources, no. That is the RAII principle.
 
S

Sachin

Hello everyone,

Suppose I have some objects created on local function stack (not on
heap). And I allocate and free all the related resources in the
constructor and destructor (e.g. memory and file handles). And I do
not implement explicit exception handling code in the function for
resource free purpose, and simply rely on destructor to free resources
if exception occurs, that is,

1. when exception occurs and the exception triggers us to go out of
current function stack to the caller to find exception handlers;
2. since objects are allocated on local stack, when exception triggers
us to go out of current stack to its caller to find exception handler,
the lifecycle of local objects on local stack will expire, and its
related destructor will be invoked and free resources.

Pseudo code like this,

Code:
void func()
{
      Class1 inst1;
      Class2 inst2;

      ... // some operations

      return;}

Does above code always safe? Are there any potential risks to leak
resources?

thanks in advance,
George

If we encounter any exception within function destructor will not be
invoked, and there are high probability of leakage I bealieve.

Thanks,
Sachin
 
R

Rolf Magnus

Sachin said:
Hello everyone,

Suppose I have some objects created on local function stack (not on
heap). And I allocate and free all the related resources in the
constructor and destructor (e.g. memory and file handles). And I do
not implement explicit exception handling code in the function for
resource free purpose, and simply rely on destructor to free resources
if exception occurs, that is,

1. when exception occurs and the exception triggers us to go out of
current function stack to the caller to find exception handlers;
2. since objects are allocated on local stack, when exception triggers
us to go out of current stack to its caller to find exception handler,
the lifecycle of local objects on local stack will expire, and its
related destructor will be invoked and free resources.

Pseudo code like this,

Code:
void func()
{
Class1 inst1;
Class2 inst2;

... // some operations

return;}

Does above code always safe? Are there any potential risks to leak
resources?

thanks in advance,
George

If we encounter any exception within function destructor will not be
invoked, and there are high probability of leakage I bealieve.

Your beliefs are wrong. If an exception is thrown, all local objects are
properly destroyed.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top