Fix various TMTag leaks
Add code for TMTag reference debugging

This code is disabled by default, and is enabled if the DEBUG_TAG_REFS
C preprocessor macro is defined.