added memory leak detection thread safety

clean up and and rebased from #418 
Ok, changed to RIAA style, but then I'm not quite sure if this additional layer is a good idea... let me know what you think?