Incorrect behavior involving critical section in Windows 2000/XP

509 Views Asked by At

Recently i tried to run an old app (written in Borland C++ Builder 6) on Windows 7 x64 and found that IO-thread was not working. Debugging showed the problem was in error-handling class not leaving critical section (indirectly, via a simple wrapper class), so every message added to log caused two calls of EnterCriticalSection but only one call of LeaveCriticalSection.

It seems to me that this error should make the class unusable, but it's one of the common classes actively used by original developer, and this particular application always worked fine in Windows 2000/XP. So the question is why this error appeared only in Windows 7?

I've read MSDN articles about critical sections and suggested questions here, the only behavior change noted is undefined acquiring order, which is definitely not the case.

1

There are 1 best solutions below

2
On

I use C++Builder 6 and my apps work fine with critical sections on Windows 7 64bit. So your problem has to be due to a bug in the app, not in the API. If a thread calls EnterCriticalSection() and successfully obtains the lock but does not call LeaveCriticalSection(), subsequent threads deadlock on their own calls to EnterCriticalSection(). So you will have to keep debugging to figure out why the original thread did not call LeaveCriticalSection() when it was supposed to. Every call to EnterCriticalSection() must be balanced with a call to LeaveCriticalSection(), especially if they are nested.