Fix a bunch of memory leaks.

This fixes a few of the memory leaks reported by coverity.