added memory leak detection thread safety

clean up and and rebased from #418 
An alternative is:

class MemoryLeakScopedMutex
{
   ScopedMutex* mutex;
public:

etc etc