Solved

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

Posted on 1998-10-29
13
9,449 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now