Fixes found with clang-analyzer

Some minor cleanups for potential double frees, etc. found with the clang-analyzer scan-build tool.