Fix memory leak in TSRM