AVSampleBufferDisplayLayerContentLayer memory leaks.

I noticed that AVSampleBufferDisplayLayerContentLayer is not released when the AVSampleBufferDisplayLayer is removed and released. It is possible to reproduce the issue with the simple code:

import AVFoundation
import UIKit

class ViewController: UIViewController {

    var displayBufferLayer: AVSampleBufferDisplayLayer?

    override func viewDidLoad() {
        super.viewDidLoad()

        let displayBufferLayer = AVSampleBufferDisplayLayer()
        displayBufferLayer.videoGravity = .resizeAspectFill
        displayBufferLayer.frame = view.bounds
        view.layer.insertSublayer(displayBufferLayer, at: 0)
        self.displayBufferLayer = displayBufferLayer

        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            self.displayBufferLayer?.flush()
            self.displayBufferLayer?.removeFromSuperlayer()
            self.displayBufferLayer = nil
        }
    }
}

In my real project I have mutliple AVSampleBufferDisplayLayer created and removed in different view controllers, this is problematic because the amount of leaked AVSampleBufferDisplayLayerContentLayer keeps increasing. I wonder that maybe I should use a pool of AVSampleBufferDisplayLayer and reuse them, however I'm slightly afraid that this can also lead to strange bugs. Edit: It doesn't cause leaks on iOS 18 device but leaks on iPad Pro, iOS 17.5.1

Did you try with iOS 17.7.2?

Hello @bastawa,

Can you provide a sample project that reproduces the issue?

-- Greg

AVSampleBufferDisplayLayerContentLayer memory leaks.
 
 
Q