HouseKeeping after Exception in a method

  • Thread starter Thread starter Asim Hussnain
  • Start date Start date
A

Asim Hussnain

We have recently found that if a funtion throws exception and caller without
fully executing, the Stack of the function is not cleared by windows i.e. if
some object was created on Stack of the called function, its desctructor
will never be called.
Consider the code example below:

void func()

{

COURObject o(params);

int* p = 0;

* p = 1; //this code will generate exception

}

void func2()

{

try

{

func();

}

catch(...)

{


}

}

In this case, destructor of COURObject would never be called To prevent
this, WE MUST USE TRY CATCH BLOCK IN THE FUCNTION WHERE WE ARE USING
COURObject.

Is it really so? I thought that stack woould always be properly cleaned up
when exception is thrown. any comments?
 
Asim Hussnain said:
We have recently found that if a funtion throws exception and caller
without
fully executing, the Stack of the function is not cleared by windows i.e.
if
some object was created on Stack of the called function, its desctructor
will never be called.
Consider the code example below:

void func()

{

COURObject o(params);

int* p = 0;

* p = 1; //this code will generate exception

}

void func2()

{

try

{

func();

}

catch(...)

{


}

}

In this case, destructor of COURObject would never be called To prevent
this, WE MUST USE TRY CATCH BLOCK IN THE FUCNTION WHERE WE ARE USING
COURObject.

Is it really so? I thought that stack woould always be properly cleaned up
when exception is thrown. any comments?

The problem is that the term "exception" is overloaded. You question is
really about C++ typed exceptions. The exception that you throw is a Win32
structured exception.

If you want to mix and match (and it is a really bad idea btw) then you need
to compile with the /EHa option.

Regards,
Will
 
Back
Top