fix some memory leaks. tnx liuch for the patch