Solved

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

Posted on 1998-10-29
13
9,718 Views
Last Modified: 2011-08-18
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
0
Comment
Question by:tanmh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +2
13 Comments
 
LVL 1

Expert Comment

by:Darren_Simons
ID: 1176549
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?
0
 
LVL 1

Expert Comment

by:Darren_Simons
ID: 1176550
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?
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 1176551
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:Answers2000
ID: 1176552
Compile with /GX- option I think
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176553
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.
0
 

Author Comment

by:tanmh
ID: 1176554
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.


0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 1176555
In this case, what about points? To whom you want put it:
Me, Nietod, Answers2000 ?
Regards, Alex
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176556
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.
0
 

Author Comment

by:tanmh
ID: 1176557
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
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176558
>> 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)
0
 

Author Comment

by:tanmh
ID: 1176559
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
0
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 50 total points
ID: 1176560
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.
0
 

Author Comment

by:tanmh
ID: 1176561
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question