Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 1998-10-29
13
Medium Priority
?
9,918 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
  • 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
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!

 
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 100 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

972 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