I think you can better abstract the part between the ENTER_CRITICAL_SECTION and the LEAVE_CRITICAL_SECTION into a separate function that has a LOCK(cs_WalletManager) at the start. This also means you don't need to explicitly leave the section prematurely in case of errors.