Some resource and memory leak fixes found by cppcheck