![](/img/trans.png)
[英]UIGraphicsGetImageFromCurrentImageContext and Swift Memory Usage
[英]Swift closure memory usage
使用闭包会导致内存压力,并且调试器会因内存问题终止应用程序。 这是我定义的简单闭包,并将其作为参数传递给不同的函数。 如果我在需要的地方用两行代码替换闭包,则内存压力消失了。 传递给函数的闭包是否会无限期保留在参数中传递的outputPixelBuffer
或sampleBuffer
?
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")
}
}
}
您正在捕获对自我的强烈引用,从而导致循环。 在关闭的参数之前添加捕获列表-[弱自我],然后在其中可以包含
let strongself = self
然后将所有对self的引用(甚至是当前隐含的)替换为strongself。 我不在可以随时编辑您的代码的设备上,但是应该这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.