Memory leak by replacing UIWindow.rootViewController
- Dominant language
- Objective-C
- Stars
- 5.7k
- Forks
- 737
- Avg merge
- 1d 20h
- Merged PRs (30d)
- 5
Description
Hi,
We noticed that our UI tests are not very stable for a while. After digging, we found that it may relate to the leak of `UIWindow.rootViewController`. Our tests will reset the root view controller in `setUp` to keep each test isolated. I noticed that the old root view controller won't be released after replacing a new one by using the memory graph and instrument allocation tracking. This behavior only happens in UI tests, not in the normal run.
Here is the sample to reproduce this case. https://github.com/inlineapps/EarlGrey/commit/3e95e24bb376b8c9a1d2201dc7a0f4f30a0bd75c
You can set a breakpoint in the `tearDown` of tests and check the memory graph. There will be 2 view controllers.
The suspect code is the method swizzle of `setRootViewController`. The last ref count +1 from our UI test code is related to this one.

We are also working on this to find a solution. Please let me know if there are any solutions. Thanks!
Contributor guide
Assessment
This issue has not been assessed yet.