Fix bug revealed by valgrind on travisci