2015-10-13 79 views
2

我正在嘗試使用AVAssetWriterInput創建一個視頻,並使用AVAssetWriterInput爲其創建一個映像的像素緩衝區。創建映像的像素緩衝區時發生內存泄漏

爲此我稱之爲功能如下,但創建幾個視頻後,應用程序收到內存警告並崩潰。我使用儀器進行了調試,看起來我有內存泄漏here

我試圖把變量pixelBufferPointer和pxData作爲類變量,並銷燬/ dealloc一旦創建視頻,但這似乎沒有任何區別。有什麼我應該做的,以釋放這種記憶?

func createPixelBufferFromCGImage(image: CGImageRef) -> CVPixelBufferRef { 
    let options = [ 
     "kCVPixelBufferCGImageCompatibilityKey": true, 
     "kCVPixelBufferCGBitmapContextCompatibilityKey": true 
    ] 

    var videoWidth = 496 
    var videoHeight = 668 

    let frameSize = CGSizeMake(CGFloat(videoWidth), CGFloat(videoHeight)) 

    var pixelBufferPointer = UnsafeMutablePointer<Unmanaged<CVPixelBuffer>?>.alloc(1) 

    var status:CVReturn = CVPixelBufferCreate(
     kCFAllocatorDefault, 
     Int(frameSize.width), 
     Int(frameSize.height), 
     OSType(kCVPixelFormatType_32ARGB), 
     options, 
     pixelBufferPointer 
    ) 

    var lockStatus:CVReturn = CVPixelBufferLockBaseAddress(pixelBufferPointer.memory?.takeUnretainedValue(), 0) 

    var pxData:UnsafeMutablePointer<(Void)> = CVPixelBufferGetBaseAddress(pixelBufferPointer.memory?.takeUnretainedValue()) 

    let bitmapinfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.NoneSkipFirst.rawValue) 
    let rgbColorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB() 

    var context:CGContextRef = CGBitmapContextCreate(
     pxData, 
     Int(frameSize.width), 
     Int(frameSize.height), 
     8, 
     //4 * CGImageGetWidth(image), 
     4 * Int(frameSize.width), 
     rgbColorSpace, 
     bitmapinfo 
    ) 

    CGContextDrawImage(context, CGRectMake(0, 0, frameSize.width, frameSize.height), image) 

    CVPixelBufferUnlockBaseAddress(pixelBufferPointer.memory?.takeUnretainedValue(), 0) 

    UIGraphicsEndImageContext() 

    return pixelBufferPointer.memory!.takeUnretainedValue() 
} 

回答

0

Unmanaged<CVPixelBuffer>泄漏。

我寧願羅素奧斯汀的答案,但我不知道如何通過pixelBufferPointerCVPixelBufferCreate沒有語法錯誤(swift noob)。如果做不到這一點,改變線

return pixelBufferPointer.memory!.takeUnretainedValue() 

return pixelBufferPointer.memory!.takeRetainedValue() 

修復泄漏

0

看看Memory leak on CIContext createCGImage at iOS 9?

我有一個類似的問題,泄漏是由於下面的代碼

CGImageRef processedCGImage = [_context createCGImage:ciImage 
                  fromRect:[ciImage extent]]; 

這裏更多背景信息:Memory Leak in CMSampleBufferGetImageBuffer

+0

感謝您的快速回復。我不知道是否它是相同的錯誤,因爲我在iOS 8.3上運行此代碼,並且我也沒有使用createCGImage – carlosb

+0

這是正確的,但我有一些嫌疑人,事實上,那些原語CVPixelBufferGetBaseAddress,CGBitmapContextCreate,CGContextDrawImage是相同的時使用使用CIContext處理createCGImage。我相信他們是相關的。 – loretoparisi

0

您不必使用UnsafeMutablePointer類型。你可能試圖從一些Objective-C例子轉換而來。我在做同樣的事情,但最終發現沒有做到這一點。嘗試

var pixelBufferPointer: CVPixelBuffer? 
... 
var status:CVReturn = CVPixelBufferCreate(
    kCFAllocatorDefault, 
    Int(frameSize.width), 
    Int(frameSize.height), 
    OSType(kCVPixelFormatType_32ARGB), 
    options, 
    &pixelBufferPointer 
) 

var pxData = CVPixelBufferGetBaseAddress(pixelBufferPointer) 

然後,你不必做takeUnRetainedValue等

您也可以嘗試使用AVAssetWriterInputPixelBufferAdaptor和像素緩衝池。這應該是更有效的。 See this example

0

當使用CVPixelBufferCreateUnsafeMutablePointer將必須爲檢索它的memory後銷燬

當我創建一個CVPixelBuffer,我不喜歡這樣寫道:

func allocPixelBuffer() -> CVPixelBuffer { 
    let pixelBufferAttributes : CFDictionary = [...] 
    let pixelBufferOut = UnsafeMutablePointer<CVPixelBuffer?>.alloc(1) 
    _ = CVPixelBufferCreate(kCFAllocatorDefault, 
          Int(Width), 
          Int(Height), 
          OSType(kCVPixelFormatType_32ARGB), 
          pixelBufferAttributes, 
          pixelBufferOut) 

    let pixelBuffer = pixelBufferOut.memory! 
    pixelBufferOut.destroy() 
    return pixelBuffer 
}