Fix namespace locking logic

While building libmarquise on my workstation I noticed a few gcc warnings. Not content to leave them alone (I took pains to remove all of them during prior coding) I went digging.

So there's some dodgy pointer handling, logic problems, and how-did-this-ever-work segfaults. They are now fixed.