Reputation: 6477
Using a closure is causing memory pressure and app to be terminated by the debugger for memory issues. Here is the simple closure I define and pass it as an argument to different functions. The memory pressure disappears if I replace the closure with two lines of code in the closure where ever it is needed. Will this closures passed to function retain outputPixelBuffer
or sampleBuffer
passed in the arguments indefinitely?
let videoProcessor: (CMSampleBuffer, CVPixelBuffer) throws -> Void = { (sampleBuffer, outputPixelBuffer) in
if let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer), CFGetTypeID(imageBuffer) == CVPixelBufferGetTypeID() {
do {
try delegate.processPixelBuffer(self, inputPixelBuffer: imageBuffer, toPixelBuffer: outputPixelBuffer)
} catch {
fatalError("Failed processing pixel buffer")
}
}
}
Upvotes: 0
Views: 263
Reputation: 1169
You're capturing a strong reference to self, causing a loop. Add a capture list - [weak self] before the parameters of the closure, then inside you can include
let strongself = self
and then replace all references to self (even currently implicit) with strongself. I'm not on a device where I can readily just edit your code, but this should do it.
Upvotes: 1