Preventing Memory Leaks Using XCTests | by Maciej Krolikowski | May, 2022

Cut back your reliance on guide assessments in iOS

Picture by William Hook on Unsplash

Whereas creating Swift apps, we’re uncovered to some dangers. One in every of them is reminiscence leaks.

Automatic Reference Counter (ARC) releases an object from the reminiscence, solely in instances the place there are not any robust references to that object. Moreover when two reference sorts have a reference to one another, non of them will ever be launched from the reminiscence, which causes a reminiscence leak.

To analyze such a difficulty we are able to use Xcode Devices or Reminiscence Graph Debugger. These instruments enable an in-depth evaluation of the occurred leaks. Nevertheless, the query we should always reply is whether or not there are any methods to really stop reminiscence leaks within the first place?

The reply is sure, by means of unit assessments. Execution of automated assessments permits catching bugs in an software earlier than passing this software on to the QA crew or the purchasers themselves. Let’s check out methods to implement such safety.

Let’s think about a easy view controller with a button.

Contained in the motion handler, a robust reference to self, causes retain cycle between button and LeakingViewController. As you’ll be able to see, even on this easy instance, we are able to create a reminiscence leak. Now let’s write a check that may alert us about a difficulty.

After making a view controller, you’ll be able to discover some mysterious block referred to as addTeardownBlock. This block is executed after exiting the present check technique — equally to the tearDown technique. It holds a weak reference to a viewController with a view to not create a retain cycle. In our instance after ending a check code, the button and the viewController will maintain one another references, making a retain cycle so addTeardownBlock is ready to entry the viewController’s reference, which in flip informs us in regards to the reminiscence leak.

Weakifying self within the button’s handler closure will do away with the reminiscence leak, satisfying the assessments on the identical time.

As you’ll be able to see, a small overlooking can result in a reminiscence leak. Let’s prolong this instance by including one other presenter class — identified from the MVP sample.

The LeakingViewControllerPresenter conforms to the LeakingViewControllerDelegate, which supplies a risk to name startLoading() technique, but in addition the LeakingViewController can name the buttonClicked() on the LeakingViewControllerPresenter. By not making the delegate property weak, we’re introducing one other retain cycle, which is confirmed by our check.

Let’s shortly repair a presenter class.

And our check begins passing once more.

Because the code begins rising there may be many different causes that may result in reminiscence leaks.

Coping with them, through the use of computerized unit testing, can considerably enhance the standard of your app and save time, that you’d in any other case need to spend on tedious guide debugging.

Even in case you are undecided in case your code will set off a retain cycle, it’s all the time good to make use of this technique to realize confidence in your code.


More Posts