Fix some memory leaks.

Just some fixes for a few memory leaks. Details in each commit. Tested on 10.9.1 (13B42).