Detect global leaks in tests

As suggested on #1391