?
Solved

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

Posted on 2010-09-14
3
Medium Priority
?
1,011 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 17

Accepted Solution

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

Assisted Solution

by:Aksh
Aksh earned 1000 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

Industry Leaders: 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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

764 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