Fix more memory leaks and potential null-pointer dereferences found by the clang static analyzer.