[#1376] [#1404] fixed some leaks reported by valgrind and cppcheck