views:

56

answers:

1

Is there any technique or tool available to detect this kind of a deadlock during runtime?

picture this in a worker thread (one of several, normally 4-6)

try
   WaitForSingleObject(myMutex);
   DoSTuffThatMightCauseAnException;
except
   ReleaseMutex(myMutex);
end;

or more generally is there a design-pattern to avoid these kind of bugs?

I coded the above code in the little hous after a longer hacking run

+6  A: 

A better coding style is to use try/finally instead of try/except for the mutex release (or any other kind of resource release, for that matter), ie:

try
  WaitForSingleObject(myMutex); 
  try 
    DoSTuffThatMightCauseAnException; 
  finally
    ReleaseMutex(myMutex); 
  end; 
except
  ...
end;
Remy Lebeau - TeamB