Fix memory leaks in test code

Change-Id: I0dbb2546f1e776ebf95b61c16ebedbce0ead4c7f