Fix most memory leaks in tablegen.

Found by the valgrind bot.