Fix memory leak in unit test (#3068)