Issue 171: Fix some problems in the pull request #204.

I fixed a deadlock and invalid memory access.