Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSCollectionView memory leak in High Sierra?

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

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 correct

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

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
}

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.

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

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?

like image 782
WatashiJ Avatar asked Jun 18 '18 20:06

WatashiJ


People also ask

What is the main cause of memory leaks?

DEFINITION A memory leak is the gradual deterioration of system performance that occurs over time as the result of the fragmentation of a computer's RAM due to poorly designed or programmed applications that fail to free up memory segments when they are no longer needed.

Is memory leak serious?

Very dangerous. Memory leaks in the kernel level lead to serious system stability issues. Kernel memory is very limited compared to user land memory and should be handled cautiously. Memory is allocated but never freed.

Where are memory leaks found?

Where are memory leaks found? Explanation: Memory leaks happen when your code needs to consume memory in your application, which should be released after a given task is completed but isn't. Memory leaks occur when we are developing client-side reusable scripting objects.

Is memory leak permanent?

Physical or permanent damage does not happen from memory leaks. Memory leaks are strictly a software issue, causing performance to slow down among applications within a given system. It should be noted a program taking up a lot of RAM space is not an indicator that memory is leaking.


1 Answers

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.

solution

like image 155
WatashiJ Avatar answered Oct 09 '22 23:10

WatashiJ