UIContextMenuConfiguration causes retain cycle

Hi dear Apple developers!

I need help to solve memory leak issue with CollectionView and UIContextMenuConfiguration I'm struggling few weeks with.

I have a fairly simple setup - Main ViewController and pushed ViewControllers with CollectionView within NavigationController.

All is Ok with memory during navigation back and forth until I use CollectionViewCell's Context Menu (calling contextMenuConfigurationForItemAt method and returning even blank ContextMenuConfiguration instance). So when I pop ViewController, objects are being kept in memory after that. I think called UIContextMenuConfiguration instance causes retain cycle. I just cannot find similar case or way to solve this.

One thing. I see deinit happens in popped ViewController but not with its CollectionView Cell.

Please help!
Please show your code generating UIContextMenuConfiguration.
`
Thanks for reply!
I've ended up commenting out everything inside and issue is still the same:

Code Block
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
        return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { [weak self] _ in
            return nil
        }
}

Thanks for showing your code and I could not reproduce the issue you described.

Whether I include your code into a sample code of CollectionView, memory consumption does not show significant difference.

One thing. I see deinit happens in popped ViewController but not with its CollectionView Cell.

One possibility, your CollectionView Cell is causing the issue.


Can you create a brand-new project, which is minimized just to reproduce the issue, and show full code of it?
Steps needed to reproduce the issue would also be welcome.
Many thanks for ideas. Appreciated.
I've checked there's nothing wrong in Cell (just imageView and label).

Yes of course I'll prepare sample project to test this functionality.

Hi!

My collectionView has drag and drop functionality + context menu for cells.

I've discovered itemsForBeginning method is being called each time after contextMenuConfigurationForItemAt method (and context menu appearing) and I've found out that the problem is in dragItem.localObject assigning.

Here's my itemsForBeginning method below:

Code Block    
Fetched objects container:
var sheets: [Sheet]?
func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
        guard let sheets = self.sheets else { return [] }
        let itemProvider = NSItemProvider(object: sheets[indexPath.item] as NSItemProviderWriting)
        let dragItem = UIDragItem(itemProvider: itemProvider)
        if indexPath.item < sheets.count {
            dragItem.localObject = sheets[indexPath.item]
            return [dragItem]
        } else {
            return []
        }
    }


UIContextMenuConfiguration causes retain cycle
 
 
Q