fix several potential memory leaks
fix all memory leaks in tests