Memory Leak in CMSampleBufferGetImageBuffer

前端 未结 2 554
旧时难觅i
旧时难觅i 2021-01-15 06:34

I\'m getting a UIImage from a CMSampleBufferRef video buffer every N video frames like:

- (void)imageFromVideoBuffer:(void(^)(UIImage* imag         


        
2条回答
  •  借酒劲吻你
    2021-01-15 06:48

    We were experiencing a similar issue in an app we created, where we are processing each frame for feature keypoints with OpenCV, and sending off a frame every couple of seconds. After a while of running we would end up with quite a few memory pressure messages.

    We managed to rectify this by running our processing code in it's own auto release pool like so (jpegDataFromSampleBufferAndCrop does something similar to what you are doing, with added cropping):

    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
    {
            @autoreleasepool {
    
                if ([self.lastFrameSentAt timeIntervalSinceNow] < -kContinuousRateInSeconds) {
    
                    NSData *imageData = [self jpegDataFromSampleBufferAndCrop:sampleBuffer];
    
                    if (imageData) {
                        [self processImageData:imageData];
                    }
    
                    self.lastFrameSentAt = [NSDate date];
    
                    imageData = nil;
                }
            }
        }
    }
    

提交回复
热议问题