[英]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.