fixed all bugs reported by the clang static analyzer

some potential leaks on error paths and some other minor thing