CoreVideo iOS memory leaks

Can someone help me track CoreVideo memory leaks when running tools in Xcode?

Basically, a memory leak occurs when I click the β€œRecord Video” button on my custom jpeg player engine. I can’t say exactly what part of my code is flowing, because Leaks Instruments does not indicate any of my calls. By the way, I'm using an iPad device to check for leaks.

Messages from leakage devices are stored here:

  • Responsible Library = CoreVideo
  • Responsible frame: CVPixelBufferBacking :: initWithPixelBufferDescription (..) CVObjectAlloc (...) CVBuffer :: Init ()

Here is my code that processes all moving jpeg frames transmitted by the server:

-(void)processServerData:(NSData *)data{ /* //render the video in the UIImage control */ UIImage *image =[UIImage imageWithData:data]; self.imageCtrl.image = image; /* //check if we are recording */ if (myRecorder.isRecording) { //create initial sample: todo:check if this is still needed if (counter==0) { self.buffer = [Recorder pixelBufferFromCGImage:image.CGImage size:myRecorder.imageSize]; CVPixelBufferPoolCreatePixelBuffer (NULL, myRecorder.adaptor.pixelBufferPool, &buffer); if(buffer) { CVBufferRelease(buffer); } } if (counter < myRecorder.maxFrames) { if([myRecorder.writerInput isReadyForMoreMediaData]) { CMTime frameTime = CMTimeMake(1, myRecorder.timeScale); CMTime lastTime=CMTimeMake(counter, myRecorder.timeScale); CMTime presentTime=CMTimeAdd(lastTime, frameTime); self.buffer = [Recorder pixelBufferFromCGImage:image.CGImage size:myRecorder.imageSize]; [myRecorder.adaptor appendPixelBuffer:buffer withPresentationTime:presentTime]; if(buffer) { CVBufferRelease(buffer); } counter++; if (counter==myRecorder.maxFrames) { [myRecorder finishSession]; counter=0; myRecorder.isRecording = NO; } } else { NSLog(@"adaptor not ready counter=%d ",counter ); } } } 

}

Here's the pixelBufferFromCGImage function:

 + (CVPixelBufferRef) pixelBufferFromCGImage: (CGImageRef) image size:(CGSize) size{ NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey, [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey, nil]; CVPixelBufferRef pxbuffer = NULL; CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, size.width, size.height, kCVPixelFormatType_32ARGB, (CFDictionaryRef) options, &pxbuffer); NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL); CVPixelBufferLockBaseAddress(pxbuffer, 0); void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer); NSParameterAssert(pxdata != NULL); CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(pxdata, size.width, size.height, 8, 4*size.width, rgbColorSpace, kCGImageAlphaNoneSkipFirst); NSParameterAssert(context); CGContextConcatCTM(context, CGAffineTransformMakeRotation(0)); CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image); CGColorSpaceRelease(rgbColorSpace); CGContextRelease(context); CVPixelBufferUnlockBaseAddress(pxbuffer, 0); return pxbuffer; 

}

Help! Thanks

+4
source share
2 answers

I reorganized the processFrame method and I no longer get leaks.

 -(void) processFrame:(UIImage *) image { if (myRecorder.frameCounter < myRecorder.maxFrames) { if([myRecorder.writerInput isReadyForMoreMediaData]) { CMTime frameTime = CMTimeMake(1, myRecorder.timeScale); CMTime lastTime=CMTimeMake(myRecorder.frameCounter, myRecorder.timeScale); CMTime presentTime=CMTimeAdd(lastTime, frameTime); buffer = [Recorder pixelBufferFromCGImage:image.CGImage size:myRecorder.imageSize]; if(buffer) { [myRecorder.adaptor appendPixelBuffer:buffer withPresentationTime:presentTime]; myRecorder.frameCounter++; CVBufferRelease(buffer); if (myRecorder.frameCounter==myRecorder.maxFrames) { [myRecorder finishSession]; myRecorder.frameCounter=0; myRecorder.isRecording = NO; } } else { NSLog(@"Buffer is empty"); } } else { NSLog(@"adaptor not ready frameCounter=%d ",myRecorder.frameCounter ); } } } 
+2
source

I do not see anything obvious. I noticed that you are using self.buffer and a buffer here. If it is saved, you can leak there. If CVPixelBufferPoolCreatePixelBuffer allocates memory in the second row after self.buffer is stored in the first row, the first may leak.

  self.buffer = [Recorder pixelBufferFromCGImage:image.CGImage size:myRecorder.imageSize]; CVPixelBufferPoolCreatePixelBuffer (NULL, myRecorder.adaptor.pixelBufferPool, &buffer); 

Hope this helps.

0
source

Source: https://habr.com/ru/post/1346265/


All Articles