【发布时间】:2020-04-30 21:25:10
【问题描述】:
我的应用程序中有一个例程,它通过 AVAssetWriter 将 MTLTextures 捕获到电影中。当我在设备上运行应用程序时,内存管理似乎保持稳定,电影被创建并写入磁盘。但是,当我在 App Store 上发布该应用程序(并下载它)时,无论电影大小如何,该应用程序都会在电影创建后不久崩溃。通过仪器运行应用程序显示在从 MTLTextures 提取 PixelBuffer 的例程中发生了巨大的内存分配。这就是崩溃发生的地方。有问题的例程在我要记录的每个 MTLTexture 的循环中运行:
func AVAssetWriterEncodeFrame(forTexture texture: MTLTexture) {
while !assetWriterVideoInput!.isReadyForMoreMediaData {
} // hang out here until isReadyForMoreMediaData == true
autoreleasepool(invoking: { () -> () in
let fps: Int32 = Int32(Constants.movieFPS)
guard let pixelBufferPool = assetWriterPixelBufferInput!.pixelBufferPool else {
print("[MovieMakerVC]: Pixel buffer asset writer input did not have a pixel buffer pool available; cannot retrieve frame")
return
}
var maybePixelBuffer: CVPixelBuffer? = nil
let status = CVPixelBufferPoolCreatePixelBuffer(nil, pixelBufferPool, &maybePixelBuffer)
if status != kCVReturnSuccess {
print("[MovieMakerVC]: Could not get pixel buffer from asset writer input; dropping frame...")
return
}
guard let pixelBuffer = maybePixelBuffer else { return }
CVPixelBufferLockBaseAddress(pixelBuffer, [])
let pixelBufferBytes = CVPixelBufferGetBaseAddress(pixelBuffer)!
// Use the bytes per row value from the pixel buffer since its stride may be rounded up to be 16-byte aligned
let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)
let region = MTLRegionMake2D(0, 0, texture.width, texture.height)
texture.getBytes(pixelBufferBytes, bytesPerRow: bytesPerRow, from: region, mipmapLevel: 0)
let presentationTime = CMTimeMake(value: Int64(frameIndexForPresentationTime), timescale: fps)
assetWriterPixelBufferInput!.append(pixelBuffer, withPresentationTime: presentationTime)
CVPixelBufferUnlockBaseAddress(pixelBuffer, [])
frameIndexForPresentationTime += frameHoldLength // set up for next frame
}) // end of autoreleasepool
} // end of func AVAssetWriterEncodeFrame(forTexture texture: MTLTexture)
在影片被写入磁盘后,图中看到的峰值无法控制地增长。分配列表显示了数以千计的条目,如下所示:
@autoreleasepool 内容。 ... 4 KiB AVFoundation。 -[AVAssetWriter 输入助手]。 (如上图所示)
有人能指出我做错了什么并希望如何解决它吗?
【问题讨论】:
-
你能告诉我们分配列表,指出正在消耗内存的活动对象的类型,以及它们的分配位置吗?
-
谢谢@warrenm。我添加了更多细节,希望能对问题有所了解。尽管我已经注意进行适当的内存管理,但我不确定为什么/在哪里没有释放或释放对象
标签: swift instruments allocation metal avassetwriter