ios – Swift Memory Leaks with UIDiffableDataSource, CFString, Malloc Blocks, and others


I’ve been writing an application for a couple months now and just started checking for memory leaks – it turns out I have a lot of them – 25 unique types (purple exclamation point) and over 500 total if I navigate through my app enough.

The Memory Graph in Xcode is pointing mostly to 1.) different elements of UIDiffableDataSource, 2.) “CFString”/”CFString (Storage)”, and 3.) “Malloc Blocks”. The Leaks Instrument is giving me similar feedback, saying about 90% of my memory leaks are these “Malloc Blocks” and also says the Responsible Frame is either newJSONString or newJSONValue. In many of the leaks there is a “4-node-cycle” involved which contains a Swift Closure Context, My UIDiffableDataSource<Item, Section> object, and __UIDiffableDataSource. The CFString ones just have one object – CFString, and nothing else. I’ll try to add images showing the 2 examples, but StackO is restricting my ability to add them because I’m a n00b.

This leads me to believe I’m creating some type of memory leak within my custom dataSource closure for the UICollectionViewDiffableDataSource. I’ve tried making the DataSource a weak var & I’ve tried using weak self and unowned self in each of my closures — especially the closure creating my datasource, but it’s had no impact.

  • What does it mean when Memory Graph/Leaks Instrument point to these generic “CFString” or “Malloc Block” objects?
  • Does anyone have an idea of what might actually be causing these memory leaks and how I can resolve them?
  • Do memory graph and Leaks instrument ever false report leaks/create unnecessary noise or are these legitimate?

any additional info or additional resources would be super helpful. So far I’ve found iOS Academy & Mark Moeykens YouTube videos helpful in understanding the basics, but having trouble applying it to my app. I can provide code blocks if that helps, but there is a lot of code that could be causing it and not really sure what all to dump in here.

overview of errors

4 node cycle (diffableDataSource)

CFString (Storage)