HouseKeeping after Exception in a method

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?
 
W

William DePalo [MVP VC++ ]

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
 

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

Top