【发布时间】:2020-02-08 15:03:23
【问题描述】:
我正在处理内存泄漏,而调试器并没有提供太多帮助。我注意到我的几个视图控制器在被解雇后仍在内存中。我的假设是有一个强有力的参考,我一直在寻找它。
- 对于我的每个课程,我喜欢包含一个记录消息的
deinit。这就是我第一次知道这些对象没有被释放的方式。 - 当我检查内存图时,我发现已关闭但仍在内存中的视图控制器。没有什么在视觉上看起来像一个保留周期。所有参考文献都是
weak var。 Xcode 没有发出警告。 - 当我通过泄漏运行 memgraph 时,它报告了
0 leaks for 0 total leaked bytes.
当我在玩泄漏时,我注意到一个名为 traceTree 的命令。我用不应该存在的对象的地址运行它。它报告了以下内容....
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
问题:我真的不知道我在看什么。这是否告诉我从WaterfallCollectionViewLayout 到CaptureViewController 有很强的引用?
CaptureViewController 是 UICollectionViewController,WaterfallCollectionViewLayout 是自定义 UICollectionViewLayout。布局类使用集合视图控制器作为布局的委托。好像是这样的……
protocol WaterfallCollectionViewLayoutDelegate: class {
func waterfallCollectionViewLayout(_ waterfallCollectionViewLayout: WaterfallCollectionViewLayout, sizeForCellAt indexPath: NSIndexPath, fitting size: CGSize) -> CGSize
}
class WaterfallCollectionViewLayout: UICollectionViewLayout {
/* ... */
weak var delegate: WaterfallCollectionViewLayoutDelegate!
/* ... */
}
任何建议或意见将不胜感激。
【问题讨论】:
-
你在使用闭包吗?
-
还要检查任何计时器或观察者....如果你添加了这些,你需要采取这些东西。
-
关闭,是的。与弱的自我相关。没有计时器。我需要更仔细地观察观察者。
标签: ios xcode memory-leaks