I'm dealing with a memory leak and the debuggers haven't been much help. I noticed that several of my View Controllers were still in memory after they were dismissed. My assumption is that there is a strong reference, and I've been searching for that.
- For each of my classes, I like to include a
deinitthat logs a message. This is how I first knew the objects weren't being released. - When I examined the memory graph, I found the View Controllers that were dismissed yet still in memory. Nothing visually looked like a retain cycle. All the references are
weak var. There were no warnings from Xcode. - When I ran the memgraph through leaks, it reported
0 leaks for 0 total leaked bytes.
As I was playing around with leaks, I noticed a command called traceTree. I ran it with the address of the object-which-shouldn't-exist. It reported the following....
2 <CaptureViewController 0x7fee7a008800> [1536]
2 <WaterfallCollectionViewLayout 0x7fee47c0b450> [368] +264: delegate 0x7fee47c0b558
2 <UICollectionView 0x7fee7a060e00> [3072] +1744: __strong _layout 0x7fee7a0614d0
1 0x7fee47c10590 [112] +24: 0x7fee47c105a8 --> offset 2
+ 1 0x7fee7b028000 [16896] +10600: 0x7fee7b02a968
+ 1 Region libobjc.A.dylib __DATA __bss: 'objc::AssociationsManager::_mapStorage' 0x7fff89c160c8
1 0x7fee67d02c30 [304] +112: 0x7fee67d02ca0
1 0x7fee67d02100 [304] +8: 0x7fee67d02108
1 0x7fee7900be00 [8704] +2432: 0x7fee7900c780
1 Region dyld __DATA __common: '_main_thread' + 800 0x1155e1060
Question: I don't really know what I'm looking at. Is this telling me that there's a strong reference from WaterfallCollectionViewLayout to CaptureViewController?
CaptureViewController is a UICollectionViewController and WaterfallCollectionViewLayout is a custom UICollectionViewLayout. The layout class uses the collection view controller as a delegate for layout purposes. It looks like this...
protocol WaterfallCollectionViewLayoutDelegate: class {
func waterfallCollectionViewLayout(_ waterfallCollectionViewLayout: WaterfallCollectionViewLayout, sizeForCellAt indexPath: NSIndexPath, fitting size: CGSize) -> CGSize
}
class WaterfallCollectionViewLayout: UICollectionViewLayout {
/* ... */
weak var delegate: WaterfallCollectionViewLayoutDelegate!
/* ... */
}
Any suggestions or advice would be greatly appreciated.