fix memory bug and leaks
Merge pull request #2 from xlc/master

fix memory bug and leaks