fix memory leak and add small test case