Fix memory leaks in test code

Change-Id: Ifa756a5da636e0b37654ad83900115872bc40f0e