fix error and mem leak found by cppcheck