Fix several memory leaks around template parsing.