1

I am trying to convert an image to CVPixelBufferRef so that is can be used further but after some seconds my app is crashing. I can see when app is starting it is using memory like 2mb but after just 15 to 20 seconds it goes upto 300mb. My function is this

- (CVPixelBufferRef) NpixelBufferFromCGImage: (CGImageRef) image
{
    int height = 300;
    int width = 400;

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                            [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
                            [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
                            nil];
    CVPixelBufferRef pxbuffer = NULL;

    CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, width,
                                      height, kCVPixelFormatType_32ARGB, (__bridge 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, width,
                                             height, 8, 4*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;
}

I'm giving an ImageRef to this function

Borys Verebskyi
  • 4,160
  • 6
  • 28
  • 42
  • 1
    Where/what is the code that frees the buffer that this function returns? – Feldur Jun 23 '16 at 07:16
  • i'm calling this CGImageRef imageRef=[myImage CGImage]; CVImageBufferRef pixelBuffer = [self pixelBufferFromCGImage:imageRef]; – Asim Shahzad Jun 23 '16 at 07:26
  • I'm not releasing the buffer i tried to release but it created a crash. Don't know where to use that I tried CFRelease(pixelBuffer); @Feldur – Asim Shahzad Jun 23 '16 at 07:28
  • 1
    If you are not releasing the buffer, then that is the source of your memory leak. You need to track down why it's crashing when you do release it. Most likely, you are using the buffer after you have released – Feldur Jun 23 '16 at 07:34

1 Answers1

0

Just try with @autoreleasepool { Your method code} Its very helpful for me in case of long operations and loops

Devil
  • 314
  • 3
  • 13
  • Tried but with no effect. – Asim Shahzad Jun 23 '16 at 07:16
  • i'm calling this function 1000 times – Asim Shahzad Jun 23 '16 at 07:18
  • How you are calling it in a loop ? if yes try it with recursive format. And put the code in same autorelasepool where to calling you same method i have tried it and it is working fine like that @autoreleasepool { CGImageRef rawImageRef = [image CGImage]; [self NpixelBufferFromCGImage:rawImageRef]; } – Devil Jun 23 '16 at 07:36