More issues reported by Coverity

The locking errors are indeed the most interesting ones. Compile tested only.