[RFC] fix several memory leaks

tested under osx 10.10 and ubuntu 14.04
Merge pull request #341 from haiwen/fix_several_memory_leaks

[RFC] fix several memory leaks