Application Verifier for locks usage

Continuing on the theme of proper lock management, there is a tool you can use to validate certain usage scenarios for your locks (i.e., critical sections).  Application Verifier contains the "Locks Verifier", which does just that.

Once Application Verifier is installed, you can find the Locks Verifier under the "Basics" tests section.  Right click on the "Locks" node to see the kind of checks performed by the verifier.

Although the Locks Verifier monitors your process for a variety of invalid usage scenarios, it will not enforce a locking hierarchy (as previously discussed in this blog).  There are some inherent difficulties with enforcing a locking hierarchy.  For example, during initialization, a locking hierarchy may be different than during normal operation.  The locks verifier has no way of knowing whether the lock is being used in such a scenario and would trigger erroneous warnings.

The Locks Verifier monitors your process for proper usage of objects such as the CRITICAL_SECTION.  Windows maintains internal data for each critical section, and improper use of these objects (such as deleting the underlying memory for one without calling DeleteCriticalSection) will corrupt the internal data.  Fortunately, this data is only used in diagnostic scenarios -- but that means diagnosing crashes and hangs with your process becomes more difficult.