UIDragItem causes memory leak when Context Menu has been initialised

Hi!

I have a CollectionView with Drag and Drop and Context Menu functionality. There's a memory leak caused by returned [UIDragItem] array in ItemsForBeginning method when I initialise cell's context menu.

Objects are being kept in memory when I pop to rootViewController. All is OK though when drag session was happened.

I've ended up commenting out everything, keeping blank UIDragItem and problem is still there:

Code Block
    func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
        let dragItem = UIDragItem(itemProvider: NSItemProvider(object: "" as NSString))
        return [dragItem]
    }

Does anybody experience this?
Looks like it's a UIKit bug...

Please help!


I also see this issue with both collection views and table views. I'm using Objective-C on Xcode 13.2 release candidate with iOS 15.2 (19C51).

In order to get a leak I have to:

  • return a non-empty array from the table/collection view delegate that asks for itemsForBeginningDragSession:
  • return a UIContextMenuConfiguration from the table/collection view delegate that asks for the contextMenuConfigurationForRow/ItemAtIndexPath
  • long press on the cell and NOT drag
  • dismiss my view controller that has the table/collection view

I then see that the collection/table view is leaked (and, in turn, its cached table view cells and the diffable data source object)

I have found that I do NOT get a leak if I do any one of these things:

  • return an empty array from the table/collection view delegate that asks for itemsForBeginningDragSession:
  • return nil from the table/collection view delegate that asks for the contextMenuConfigurationForRow/ItemAtIndexPath
  • after the long press, begin dragging

It does not matter whether the drag is dropped or cancelled, just starting to drag will prevent the leak.

Returning a non-nil UIContextMenuConfiguration which has blocks for its previewProvider and actionProvider that return nil (which is visually the same as returning a nil configuration - no context menu is shown on a long press) will still leak.

Did you solve this problem?

UIDragItem causes memory leak when Context Menu has been initialised
 
 
Q