MINOR: fix a few memory usage errors

These are either use after free errors or small leaks where memory
is not free'd after some error state is detected.