Fix memory leak issue, added test file