fixed some clang reported potential memleak and use-after-free errors