fix a few memory leakage issues

Hi this pull request is to fix the memory leakage issues, while cleanup the trailing blank spaces in the source code. Thanks