Fix potential memleak and use-after-free errors as reported by clang

Signed-off-by: Anthony G. Basile <blueness@gentoo.org>