Fix various potential memory leaks.

Though I expect most not to be exercised in practice.