Fix various memleaks in the tests

Fix the memory leaks in the test suite to better detect the memory leaks occurring in the actual library code.