Fix memory leaks in tests