• C

Porting __leave... What does __leave do anyway?

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... ;-(
asciimanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

asciimanAuthor Commented:
0
AlexFMCommented:
__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
AlexFMCommented:
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
Prepare for an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program curriculum features two internationally recognized certifications from the EC-Council at no additional time or cost.

asciimanAuthor Commented:
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
AlexFMCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
srinimsCommented:
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
asciimanAuthor Commented:
Thanks everyone for your help. I think I've learned something today.

But nothing beats clean programming. :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.