Fix the memory leaks in the tests