who has that object locked?

  • Thread starter Thread starter cs
  • Start date Start date
C

cs

I have an app that freezes on trying to lock an object, probably cause
someone else has the lock and they aint giving it back. How can I find out
who has that lock?
 
cs,

You can't really, as that information is not exposed. You need to track
down the deadlock and remove it. The information on what thread holds the
lock is kept internally (by the Monitor class, most likely).

Hope this helps.
 
cs said:
I have an app that freezes on trying to lock an object, probably cause
someone else has the lock and they aint giving it back. How can I find out
who has that lock?

I can't help you solve that, but there is a lesson here: do not lock
publicly visible objects. code like

lock(this) {
...
}

is inherently dangerous, since any code that can see "this" can also lock
it, potentially causing deadlocks, hangs, race conditions, etc. A much
better choice is:

private Object lock = new Object;

lock(this.lock) ...

Now you know that any code which locks the object is in the same class.
Even if you provide a public accessor for the lock (usually unnecessary and
generally a bad idea):

public Object Lock {
get { return lock; }
}

you can now add tracing here to track down the culprit.

In other words, monitors are not an exception to the rule that public
mutable fields are a bad idea.
 
Back
Top