Small fixes

Fix some errors found by cppcheck.
Most errors only happen in error code path and will lead to memory leaks.