Fix memory leaks reported by cppcheck