fix bugs reported by running test with valgrind
Merge pull request #24 from manuzhang/fix_valgrind_errors

fix bugs reported by running test with valgrind