What is the best practice (cs is a TCriticalSection)
I have seen this a lot
cs->Enter();
try {
}
__finally {
cs->Leave();
}
but why not enter the critical section within the try block? Could it cause any issues?
try {
cs->Enter();
}
__finally {
cs->Leave();
}
Do not call
Leave()unlessEnter()succeeds. IfEnter()fails, callingLeave()could leave the cs in a bad state. This is generally the same rule you should follow for any code that needs to usetry..__finallyto manage resources. Allocate/Obtain the resource first, THEN enter thetryblock. Or else change the code to utilize RAII-based logic instead or usingtry..__finallyat all.