Test for memory leaks in CI

Thanks to all of you for getting back to me!

It seems like we have a few different options here. We test on both macOS and Linux in CI so I suppose we could just use leaks and only test on macOS. But a cross-platform solution is appealing.

In particular something we can run with our test targets is really great in order to allow us to re-use our test suite and high code coverage rather than writing new tests for this purpose. @johannesweiss, I imagine it might be possible for us to adapt your allocation counter tool to run test targets?

For now I was able to use your example above with an atExit handler to use leaks in our test targets, and it correctly found the leak I referenced in the original post as well as one other which I agree is a leak. So at least in my case it appears things do get cleaned up normally @Jon_Shier.

On Linux, that shouldn't be that hard. The measure function really just wrap the code that is run with resetting/reading the atomic counters. So it should be possible to do the reset before the first test runs (for example with a __attribute__((constructor)) C function) and do the reading/interpretation after the last test ran (for example with atexit).

On macOS on the other hand, it would be harder because the way we use the DYLD_INTERPOSE feature which has pretty odd requirements: It only works if DYLD_INTERPOSE is used in a .dylib (and not from the main app) but it also has to be a .dylib that's linked directly into the main binary (using dyld's LC_DYLIB which is what you get if you compile with -lmyLib). AFAIK, xctest works by dlopening your .xctest bundle as a .dylib so DYLD_INTERPOSE won't work I think so the counters will return pretty much all 0 I think.

1 Like

Turns out there were leaks within Alamofire itself (whoops :grimacing:) and our test suite as well. Fixing those wasn't too bad, I just wish there was a way to filter by origin in the leaks output, as we appear to be picking up a bunch of Foundation leaks (only 36KB, so not too bad).

This will certainly be a feature request for Xcode 12.

2 Likes