A memory leak should be considered a bug. Therefore, it makes sense to fail tests when they contain memory leaks. On my machine, all tests pass with this change (I fixed one remaining issue already).
This patch adds a new --fail-on-memleak flag. When it is used, Blender will have a non-zero exit code when guardedalloc detects a memory leak.
I enabled this flag in various places that run tests. I also added --debug-memory, because it adds more information to the test output.
If a test fails, one should rerun the test with ctest -R testname --verbose. The output will contain the names of the memory blocks that haven't been freed.