[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Porting __leave... What does __leave do anyway?

Posted on 2006-03-29
7
Medium Priority
?
1,141 Views
Last Modified: 2008-02-01
I'm trying to compile a friends C code, but I've run into a __leave macro that seems to be Visual studio specific, which I don't have. I've been searching on the internet, but that only added to my conmfusion.

What I want to know is:
- What does __leave do anyway? Does or does it not call __finally?
- What kind of a hack can I use best to circumvent this macro?

I love Microsoft... ;-(
0
Comment
Question by:asciiman
  • 3
  • 3
7 Comments
 

Author Comment

by:asciiman
ID: 16321832
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16321992
__try
{
   // optionally: __leave: go to end of __try block (loke goto)
}
__finally ( expression )
{
}

This is Microsoft-specific Win32 exception handling which can be used both in C++ and C programs. Replace it with C++ excception handler. The same is recommended by Microsoft:
"You can ensure that your code is more portable by using C++ exception handling. Also, C++ exception handling is more flexible, in that it can handle exceptions of any type. For C++ programs, it is recommended that you use the C++ exception-handling mechanism (try, catch, and throw statements)."

0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16322038
Actually, you are writing C code and cannot use exceptions. In this case you cannot catch exceptions like null-pointer assignment etc..
BTW, __finally is executed after __leave:
The effect of __leave is to jump to the end of the try-finally block. The termination handler is immediately executed.

What to do instead of this? Just don't write with bugs :) For example:

int* p;
p = 0;

__try
{
    *p = 0;
}
__finally
{
   ...
}

You can always write:

if ( p != 0 )
   *p = 0;
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:asciiman
ID: 16322367
LOL. Wouldn't we all want to write bug-free...

The goto statement compiles ok. That's at least something.

But does "the termnination handler is called" doesn't that mean that an exception is thrown? If I remove __leave, must I invoke something like exit(1) in the __finally section?
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 1400 total points
ID: 16322593
sorry, __finally is not exception handler, this is termination handler. Actually, __try - __finally is not used for exceptions. There is another block: __try - __except, which is used for exceptions. My last post about bugs is valid for __try - __except.

About __try - _finally:

__try
{
   // code 1
   
   if ( ... )
      __leave;

   // code 2

   if ( ... )
      __leave;

   // code 3
}
__finally(true)
{
   // code 4
}

is equal to:

   // code 1
   
   if ( ... )
      goto clean;

   // code 2

   if ( ... )
      goto clean;

   // code 3

clean:
   // code 4

__finally is also executed when any code inside of __try block calls return statement.
If __try - __finally block is executed inside of __try - __except block, and there is exception inside of __try - __finally block, first __finally is executed, and then __except.

In C you can reproduce __try - __finally using standard program logic, and cannot reproduce __try - __except.
0
 
LVL 3

Expert Comment

by:srinims
ID: 16329993
the goto statement will causes stack unwinding.
and that will take care of destruction of all automatic objects created in the try block.
and as part of stack unwinding, the asociated finally block also will get execution.
0
 

Author Comment

by:asciiman
ID: 16330046
Thanks everyone for your help. I think I've learned something today.

But nothing beats clean programming. :)
0

Featured Post

Technology Partners: 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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Suggested Courses

872 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