Link to home
Start Free TrialLog in
Avatar of tanmh
tanmh

asked on

Error C2712: Cannot use __try in functions that require object unwinding

Hi,
I tried out __try and __finally in my codes and when i compile, i have the following error:
Error C2712: Cannot use __try in functions that require object unwinding

What does object unwinding mean? How would I know if my functions require object unwinding? The help file says that to get around this problem, compile with /GX. What happens if we "get around" this problem? Will it affect my program?

Please advise.

Andrea
Avatar of Darren_Simons
Darren_Simons

AIUI when a function throws an exception it immediately leaves that function. If during that function you have allocated some memory which does not automatically deallocate (eg. creating pointers) the memory will not be deallocated. This, I believe, is object unwinding.

Would it be possible to change the way you have allocated the memory?
AIUI when a function throws an exception it immediately leaves that function. If during that function you have allocated some memory which does not automatically deallocate (eg. creating pointers) the memory will not be deallocated. This, I believe, is object unwinding.

Would it be possible to change the way you have allocated the memory?
Why you use __try in C++? My compilator (Borland) make me
error and demand  try/catch code. Example:

  try
  {
    dc.SelectObject(hPen);
  }
  catch (const char *s)
    {
      throw TXOwl(s);
    }
It seems me, that can't use __try in C++!
Regards, Alex.
Compile with /GX- option I think
To the best of my knowledge the /GX option will make the structured (windows) exception work more like a C++ exception and cause it to delete local objects as it unwinds the stack.  If you have objects, you should use this option.

What does object unwinding mean?

With the structured (windows not C++) exceptions, when an exception is thrown Windows removes the stack frame (local variables)  for each function it throws out of.  That is it removes local variables until it finaly gets back to a place where the exception is caught.  However, it doesn't know what these local variables are, it just is removing them from the stack like they are dumb data.   This is fine if they are chars, ints, or dumb pointers.  But if they are objects, their destructors should be called so they can "clean up"  Windows can't do that though, it doesn't know about destructors.  In a regular C++ exception, the destrcutors will be called, which is why you should use C++ exceptions whereaver possible.  Using the /GX option is a good compromize, the compiler will try to destroy objects as it handles the windows exception, thus it will work more like a C++ exception.

Let me know if you have questions.
Avatar of tanmh

ASKER

hi all experts,
let me answer your questions accordingly:

Darren_Simons : I only have file ptrs which I obtained using:
ifstream ifp (<mypath>); I didn't allocate any memory in this function.

Nietod, Answers2000 : I checked my compiler options and realised
it's already /GX. There was a warning recommending that I should use try-catch instead, as Alex pointed out.


In this case, what about points? To whom you want put it:
Me, Nietod, Answers2000 ?
Regards, Alex
Note that the C++ exception system is for handling  exceptions generated in your code only.  It does not handle exceptions generated by the operating system (structured exceptions).    For example if you pass an invalid pointer to a windows procedure, it will generate a structured exception, not a C++ exception.  Thus you can not convert from one mechanism to the other and expect the code to work the same, they were designed for different purposes.

However, it is possible to convert the structured exception to a C++ exception, if you would like.   Then you can use C++ exception only in your code.  Look at the _set_se_translator() function documentation.
Avatar of tanmh

ASKER

Hi Alex & Nietod,
Thanks for your advice. I'll try them out.
As for the points allocation, I intend to allocate to both of you. Please advise how i can do it.

thanks.
andrea
>> I intend to allocate to both of you
there is no provision for spliting points in EE.  You have to choose which expert helped the most.  or, if you have points to spare, and if you really feel that two experts helped equally, you can ask a 2nd "dummy" question for the other expert to answer.  (Note remember to put that experts name on the question so no one else answeres it)
Avatar of tanmh

ASKER

hi,
in this case, i will allocate these pts to Alex and Nietod can I ask u a new question?

So Alex, I will reopen the question to you.

Regards,
Andrea
ASKER CERTIFIED SOLUTION
Avatar of AlexVirochovsky
AlexVirochovsky

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tanmh

ASKER

Hi Alex,
can i ask clarify another thing with you?
the help file suggested something like
try {
}
catch {
delete a[];
}
delete a[];

can i do something like
try {
}
catch {
delete a[];
}
.
try {
.}
catch{
delete b[];
}
.
delete a[];
delete b[];

thanks.