NSSierraView High Sierra中的内存泄漏? [英] NSCollectionView memory leak in High Sierra?

查看:56
本文介绍了NSSierraView High Sierra中的内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到通过Instruments在NSCollectionView中发生内存泄漏.当我查找代码时,它显示下面的特定行:

I have noticed a memory leak in NSCollectionView through Instruments. When I track down to the code, it shows the specific line below:

collectionView.makeItem(withIdentifier: identifier, for: indexPath) as? DisplayableCellProtocol

然后我在内存调试器Xcode中查看了它,发现有一些未引用的项目导致了泄漏.但是,并非 makeItem 创建的所有项目都在泄漏,其中有些是正常的,但有些甚至没有显示.

Then I looked it in Xcode, memory debugger, and find out there are some non-referenced items that caused the leak. However, not all items created by makeItem is leaking, some of them are normal, but some are not even shown.

受管理的普通未泄漏项目类似于此图

Managed normal unleaked item is like this graph

泄漏的是这样的(没有任何连接):

And the leaked ones are like this (without any connections):

那正常吗,其他人也有同样的问题吗?有谁知道如何正确解决这个问题?这与使用xib设计项目视图有关吗?

Is that normal, does anyone else has the same problem? Does anyone know how to correctly solve this problem? Does this have anything to do with using xib to design the items views?

以下一些代码可能有助于您了解这种情况:

Here are some code that may be helpful to understand the situation:

func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  let data = datasource[indexPath.item]
  let identifier: String = "ServiceCell"
  // Next line is where the leak occurs
  guard let cell = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: identifier), for: indexPath) as? ServiceCell else {
      return ServiceCell(nibName: NSNib.Name("ServiceCell.xib"), bundle: Bundle.main)
  }
  cell.iconView.image = data.icon
  cell.serviceLabel.stringValue = data.name
  cell.introLabel.stringValue = data.content
  cell.highlighted = false
  return cell
}

ServiceCell的定义是:

The definition of ServiceCell is:

class ServiceCell: NSCollectionViewItem {
  @IBOutlet weak var iconView: NSImageView!
  @IBOutlet weak var serviceLabel: NSTextField!
  @IBOutlet weak var cmdLabel: NSTextField!
  @IBOutlet weak var introLabel: NSTextField!

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do view setup here.
  }
}

不确定该代码是否对您有所帮助.我曾尝试查找自己的代码中是否有任何错误,但尚未找到任何错误.

Not sure if the code is helpful here. I have tried to find if there is any bug in my own code, but have not found any yet.

与此同时,我发现了许多其他泄漏,其中大多数都指向 makeItem

Meanwhile, I found a lot of other leaks, and most of them points to the makeItem line

更新:我再次浏览了一下.因此,每次它将使实际需要的项目数量增加一倍.例如,我需要2个单元格,它将创建4个而不是2个,其中两个是泄漏的单元格.有什么想法吗?

Update: I have looked through it again. So every time it will double the number of items that is actually needed. For example, I need 2 cells, it will create 4 instead of 2, and two of them is the leaked ones. Any ideas?

推荐答案

终于解决了.在创建从xcode继承自 NSCollectionViewItem 的类并创建xib文件时,默认情况下,将xib中的 File Owner 的类设置为之前创建的子类.当我们在xib中添加自定义对象时,需要将其设置为空.

This is finally solved. When creating a class inherited from NSCollectionViewItem with creating an xib file, the class of the File Owner in the xib is set to the subclass created before by default. When we add a custom object in the xib, this needs to be set empty.

这篇关于NSSierraView High Sierra中的内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆