Solved

C# - try-catch (with return) in lock

Posted on 2010-09-14
3
985 Views
Last Modified: 2012-05-10
Hello,

I'm looking at the IL of a function similar to this and it looks like when an exception occurs the lock is not released.

Is this correct?

Can anybody elaborate?

P.S. I know there are other ways of doing this but I want to know why the lock is not being released. (or at least doesn't seem to be being released)


int TestFunc()
{
lock(object)
{
try
{
...
return 1;
}
catch(Exception)
{
return -1;
}
}
}

Open in new window

0
Comment
Question by:mac-will
3 Comments
 
LVL 17

Accepted Solution

by:
Daniel Reynolds earned 250 total points
ID: 33676502
swag.... lock is released as it goes out of scope.
0
 
LVL 6

Assisted Solution

by:Aksh
Aksh earned 250 total points
ID: 33676713
Forwarding the thought what xDJR1875 has mentioned...

lock(...) totally dependent on the scope. If the program goes out of the scope then "implicitly" it gets released.

Now lets try to find graceful solution to your problem. If the lock(...) encompassed code that is prone to throw an exception then you should use Monitor.Enter/Exit method because "A lock method call internally calls Monitor.Enter(..)"

Your Code with Monitor.Enter/Exit and FINALLY block.

=========================================
int TestFunc()
{
Monitor.Enter(object) //Explicit calling rather than implicit lock-ing
{
try
{
   ...
    return 1;
}
catch(Exception)
{return -1;}

finally { //releasing the lock explicitly
Monitor.Exit(object);
}

}
}

==========================================================

0
 
LVL 8

Author Comment

by:mac-will
ID: 33676844
Thanks people.
However I know how it is suppose to work but I wasn't getting the expected behaviour.

I found this and I think this may be what I am seeing.

http://blogs.msdn.com/b/ericlippert/archive/2009/03/06/locks-and-exceptions-do-not-mix.aspx
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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