Initialise the memory manager properly and fix some memory leaks in testing