我有一个 super 简单的基本应用程序,
只有 1 个带有 collectionView 的 View Controller
它似乎泄漏了我的 collectionViewFlowLayout,我不知道为什么
Leaked Object # Address Size Responsible Library Responsible Frame
__NSDictionaryM 1 0x7fcc4ca31340 48 Bytes UIKit UICollectionViewLayoutCommonInit
__NSDictionaryM 1 0x7fcc4ca313a0 48 Bytes UIKit UICollectionViewLayoutCommonInit
__NSDictionaryI 1 0x7fcc4ca31570 64 Bytes UIKit UICollectionViewFlowLayoutCommonInit
__NSDictionaryM 1 0x7fcc4ca313d0 48 Bytes UIKit UICollectionViewLayoutCommonInit
UICollectionViewFlowLayout 1 0x7fcc4ca310c0 512 Bytes leak-application ObjectiveC.UICollectionViewFlowLayout.__allocating_init (ObjectiveC.UICollectionViewFlowLayout.Type)() -> ObjectiveC.UICollectionViewFlowLayout
__NSDictionaryM 1 0x7fcc4ca31370 48 Bytes UIKit UICollectionViewLayoutCommonInit
__NSDictionaryM 1 0x7fcc4ca31400 48 Bytes UIKit UICollectionViewLayoutCommonInit
NSMutableIndexSet 1 0x7fcc4ca314e0 48 Bytes UIKit UICollectionViewLayoutCommonInit
NSMutableIndexSet 1 0x7fcc4ca31470 48 Bytes UIKit UICollectionViewLayoutCommonInit
__NSDictionaryM 1 0x7fcc4ca2f290 48 Bytes UIKit UICollectionViewLayoutCommonInit
这是我的代码
class ParallaxCollectionViewCell: UICollectionViewCell {
let titleLabel = UILabel(frame: CGRectMake(0, 0, 100, 100))
override init(frame: CGRect) {
super.init(frame: frame)
self.contentView.addSubview(titleLabel)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
var collectionView: UICollectionView?
let collectionViewLayout = UICollectionViewFlowLayout()
override func viewDidLoad() {
super.viewDidLoad()
setup()
}
func setup() {
self.collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: self.collectionViewLayout)
self.view.addSubview(self.collectionView!)
self.collectionView?.delegate = self;
self.collectionView?.dataSource = self;
self.collectionView?.backgroundColor = UIColor.whiteColor()
self.collectionView?.registerClass(ParallaxCollectionViewCell.self, forCellWithReuseIdentifier: "identfifier")
self.collectionView?.reloadData()
}
override func viewDidLayoutSubviews() {
self.collectionView?.frame = self.view.bounds
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(CGRectGetWidth(self.view.bounds), 100)
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let parallaxCell = collectionView.dequeueReusableCellWithReuseIdentifier("identfifier", forIndexPath: indexPath) as ParallaxCollectionViewCell
parallaxCell.titleLabel.text = "test"
return parallaxCell
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
}
如果我将 collectionViewLayout 属性设为可选的 var 并在设置中分配它
var collectionViewLayout : UICollectionViewFlowLayout?
func setup() {
self.collectionViewLayout = UICollectionViewFlowLayout()
self.collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: self.collectionViewLayout!)
...
它不会泄漏..但这不是解决方案吗?
最佳答案
我之前遇到过一个与 NIB 加载相关的 Swift 错误,它会导致属性被初始化两次。如果我没记错的话,在某些情况下,可能会在同一个对象实例上调用两个不同的指定初始化器,导致对象的实例属性被初始化两次,并在第一次初始化期间实例化任何东西。 (那是在 Cocoa 应用程序中使用 NSWindowController 的早期 Swift 测试版。我不知道当前 Swift 版本中是否仍然存在该错误。)
这可以解释您在 collectionViewLayout
属性中看到的泄漏,以及为什么当您在 setup()
中分配属性时它不会泄漏。
您可以使用 Instruments 来查看创建了多少个 UICollectionViewFlowLayout
实例,或者您可以在 -[UICollectionViewFlowLayout init]
中设置一个符号断点以查看它是否被调用两次你的 ViewController
初始化。
否则,它可能只是检漏仪的误报。
要解决此问题,您可以尝试覆盖 init(coder:)
并在该方法中初始化您的 collectionViewLayout 属性:
required init(coder aDecoder: NSCoder) {
self.collectionViewLayout = UICollectionViewFlowLayout()
super.init(coder:aDecoder)
}
但是,您需要确认此初始化程序未被调用两次。
关于ios - 无法解释的内存泄漏 swift iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27105185/