iOS 14.5 の CoreML メモリリークについて質問する

iOS 14.5 の CoreML メモリリークについて質問する

私のアプリケーションでは、オブジェクト検出のためにカスタム MLModel を備えた VNImageRequestHandler を使用しました。

アプリはiOSバージョン14.5より前であれば問題なく動作します。

iOS 14.5 がリリースされたとき、すべてが壊れてしまいました。

  1. try handler.perform([visionRequest])エラーが発生するたびに(Error Domain=com.apple.vis Code=11 "encountered unknown exception" UserInfo={NSLocalizedDescription=encountered unknown exception})、pixelBufferメモリが保持されて解放されないため、AVCaptureOutput のバッファがいっぱいになり、新しいフレームが来なくなります。
  2. 以下のようにコードを変更し、pixelBuffer を別の変数にコピーすることで、新しいフレームが来ない問題は解決しましたが、メモリ リークの問題は依然として発生します。

ここに画像の説明を入力してください

ここに画像の説明を入力してください

ここに画像の説明を入力してください

メモリリークのため、しばらくするとアプリがクラッシュしました。

iOS バージョン 14.5 より前では、検出は完全に機能し、try handler.perform([visionRequest])エラーは発生しないことに注意してください。

これが私のコードです:

private func predictWithPixelBuffer(sampleBuffer: CMSampleBuffer) {
  guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
    return
  }
  
  // Get additional info from the camera.
  var options: [VNImageOption : Any] = [:]
  if let cameraIntrinsicMatrix = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, nil) {
    options[.cameraIntrinsics] = cameraIntrinsicMatrix
  }
  
  autoreleasepool {
    // Because of iOS 14.5, there is a bug that when perform vision request failed, pixel buffer memory leaked so the AVCaptureOutput buffers is full, it will not output new frame any more, this is a temporary work around to copy pixel buffer to a new buffer, this currently make the memory increased a lot also. Need to find a better way
    var clonePixelBuffer: CVPixelBuffer? = pixelBuffer.copy()
    let handler = VNImageRequestHandler(cvPixelBuffer: clonePixelBuffer!, orientation: orientation, options: options)
    print("[DEBUG] detecting...")
    
    do {
      try handler.perform([visionRequest])
    } catch {
      delegate?.detector(didOutputBoundingBox: [])
      failedCount += 1
      print("[DEBUG] detect failed \(failedCount)")
      print("Failed to perform Vision request: \(error)")
    }
    clonePixelBuffer = nil
  }
}

同じ問題を経験した人はいますか? もしそうなら、どのように解決しましたか?

ベストアンサー1

開発者ポータルで入手可能な iOS 14.7 ベータ版ではこの問題は修正されているようです。

おすすめ記事