Solved

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

Posted on 1998-10-29
13
9,609 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fast, free, private online code repository / editor 7 107
Unable to start eclipse ? 17 154
Eclipse IDE - Cannot copy/paste from console output 8 263
Issues with C++ Class 19 101
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

821 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