Fix memory leak at exit in tests