Re: is such exception handling approach good?



On Fri, 21 Dec 2007 04:44:01 -0800, George
<George@xxxxxxxxxxxxxxxxxxxxxxxxx> 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

it sounds as if you are doing some stuff in some constructor...

it is normally not a good idea to do operations in the ctor that could
cause an exception. the reason for this is that if an exception is
ever thrown in your ctor you end up with an invalid object. therefore
it is better to have some other method to initialize such with and
handle a failure outside the ctor like bool init() or something.

hth/ajk
.



Relevant Pages

  • Re: is such exception handling approach good?
    ... Suppose I have some objects created on local function stack. ... destructor. ... exception handling code in the function for resource free purpose, ... rely on destructor to free resources if exception occurs, that is, ...
    (microsoft.public.vc.language)
  • is such exception handling approach good?
    ... Suppose I have some objects created on local function stack. ... destructor. ... exception handling code in the function for resource free purpose, ... rely on destructor to free resources if exception occurs, that is, ...
    (microsoft.public.vc.language)
  • Re: Non-failure guarantied malloc/new
    ... After all I can recieve such a exception at any time so I ... If clean-up of resources is performed by destructors, ... performed automatically as the stack is unwound. ... memory is not available. ...
    (comp.lang.cpp)
  • Re: Try Finally...
    ... Now we can protect every more or less specialized action ... an error indication (exception or error code) has to be ... an according exception handler can be inserted into the code. ... resources, as they are related to specific actions. ...
    (comp.lang.pascal.delphi.misc)
  • Re: x86 exception handling and stack demand
    ... When an exception occurs that will be passed down to user mode as an SEH exception, the kernel arranges for control to return to user mode at a special function in NTDLL, with several parameters on the stack containing information about the exception. ... In XP and later, the system stores a pointer to the initial stack allocation block in the TEB that is used by the kernel to decommit the stack via NtFreeVirtualMemory when the thread is terminated in a non-graceful fashion, closing this leak. ...
    (microsoft.public.win32.programmer.kernel)