fixed a memory leak and cppcheck errors