[RFC] fix several memory leaks

tested under osx 10.10 and ubuntu 14.04