is such exception handling approach good?



 DEVELOP > c-Plus-Plus > is such exception handling approach good?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "George2"
Date: 21 Dec 2007 06:44:15 AM
Object: is such exception handling approach good?
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;
}
[/Code]
Does above code always safe? Are there any potential risks to leak
resources?
thanks in advance,
George
.

User: "Rolf Magnus"

Title: Re: is such exception handling approach good? 21 Dec 2007 06:59:55 AM
George2 wrote:

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;
}
[/Code]

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

User: "Sachin"

Title: Re: is such exception handling approach good? 24 Dec 2007 12:18:24 AM
On Dec 21, 5:44=A0pm, George2 <george4acade...@yahoo.com> wrote:

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()
{
=A0 =A0 =A0 Class1 inst1;
=A0 =A0 =A0 Class2 inst2;

=A0 =A0 =A0 ... // some operations

=A0 =A0 =A0 return;}

[/Code]

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
.
User: "Rolf Magnus"

Title: Re: is such exception handling approach good? 24 Dec 2007 03:53:49 AM
Sachin wrote:

On Dec 21, 5:44 pm, George2 <george4acade...@yahoo.com> wrote:

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;}

[/Code]

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



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER