add test cases for catching memory leaks
fix memory leaks