Well, there are many, many things you have to check. Here are a few ones, based on your examples:
Check that at least one of the variables used in the loop condition is modified by the loop body. If not, you're looking at an infinite loop.
Also look out for stray semicolons, as in:
for (int i = 0; i < length; ++i);
{
// This code will only run once.
}
Don't introduce these bugs in the first place by using RAII throughout your project. Most, if not all, languages have some kind of construct that guarantees resources are freed when leaving the scope (e.g. C++'s destructors, C#'s using
, Python's with
, Common Lisp's (unwind-protect)
, etc.).
- Missing locks in concurrent variable access
Hopefully the shared variables are clearly visible (e.g. they share some naming convention, or there's an up-to-date list somewhere, or they're all defined in the same module). Then you can grep the source for those variables and verify locks are put in place around all stores to them.
Of course, it's always better to encapsulate write access to shared variables inside functions or methods or whole classes that perform the necessary locking themselves. See Herb Sutter's Active Objects for an very good abstraction of this concept.