OStack程序员社区-中国程序员成长平台

标题: ios - Swift 3 中的 vImageBuffer_InitWithCGImage 内存泄漏 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:54
标题: ios - Swift 3 中的 vImageBuffer_InitWithCGImage 内存泄漏

我正在尝试进行直方图计算。一切正常,除了以下方法在 Instruments 中分析时显示出巨大的内存泄漏。

每次调用以下方法时,它使用 200-300 MB 的内存并且从不释放:

       func histogramCalculation(_ imageRef: CGImage) -> (red: [UInt], green: [UInt], blue: [UInt]) {

            var inBuffer = vImage_Buffer() 

            vImageBuffer_InitWithCGImage(
                &inBuffer,
                &format,
                nil,
                imageRef,
                UInt32(kvImageNoFlags))

            let alpha = [UInt](repeating: 0, count: 256)
            let red = [UInt](repeating: 0, count: 256)
            let green = [UInt](repeating: 0, count: 256)
            let blue = [UInt](repeating: 0, count: 256)

            let alphaPtr = UnsafeMutablePointer<vImagePixelCount>(mutating: alpha) as UnsafeMutablePointer<vImagePixelCount>?
            let redPtr = UnsafeMutablePointer<vImagePixelCount>(mutating: red) as UnsafeMutablePointer<vImagePixelCount>?
            let greenPtr = UnsafeMutablePointer<vImagePixelCount>(mutating: green) as UnsafeMutablePointer<vImagePixelCount>?
            let bluePtr = UnsafeMutablePointer<vImagePixelCount>(mutating: blue) as UnsafeMutablePointer<vImagePixelCount>?

            let rgba = [redPtr, greenPtr, bluePtr, alphaPtr]

            let histogram = UnsafeMutablePointer<UnsafeMutablePointer<vImagePixelCount>?>(mutating: rgba)
            let error : vImage_Error = vImageHistogramCalculation_ARGB8888(&inBuffer, histogram, UInt32(kvImageNoFlags))

            if (error == kvImageNoError) {
                return (red, green, blue)
            }
            return (red, green, blue)
}

这里有什么问题.....



Best Answer-推荐答案


vImageBuffer_InitWithCGImage 的文档解释:

You are responsible for returning the memory referenced by buf->data to the system using free() when you are done with it.

所以我希望按照以下方式清理内存:

inBuffer.data.deallocate(bytes: inBuffer.rowBytes * Int(inBuffer.height),
                         alignedTo: MemoryLayout<vImage_Buffer>.alignment)

附带说明,您在此处使用 UnsafeMutablePointer 并不安全。例如,没有保证 alpha 在您使用引用时会存在。允许 Swift 在您创建 alphaPtr 后立即销毁 alpha(因为它再也不会被引用)。您很少想使用 UnsafeMutablePointer.init。相反,您想使用 withUnsafe... 方法来建立有保证的生命周期。例如(未经测试,但可以编译):

var alpha = [vImagePixelCount](repeating: 0, count: 256)
var red = [vImagePixelCount](repeating: 0, count: 256)
var green = [vImagePixelCount](repeating: 0, count: 256)
var blue = [vImagePixelCount](repeating: 0, count: 256)

let error = alpha.withUnsafeMutableBufferPointer { alphaPtr -> vImage_Error in
    return red.withUnsafeMutableBufferPointer { redPtr in
        return green.withUnsafeMutableBufferPointer { greenPtr in
            return blue.withUnsafeMutableBufferPointer { bluePtr in
                var rgba = [redPtr.baseAddress, greenPtr.baseAddress, bluePtr.baseAddress, alphaPtr.baseAddress]
                return rgba.withUnsafeMutableBufferPointer { buffer in
                    return vImageHistogramCalculation_ARGB8888(&inBuffer, buffer.baseAddress!, UInt32(kvImageNoFlags))
                }
            }
        }
    }
}

关于ios - Swift 3 中的 vImageBuffer_InitWithCGImage 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42055549/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4