Fix memory leaks.
Merge pull request #1030 from ktf/fix-memory-leaks

Misc fixes -- Fix memory leaks.