0

I am trying to create CVPixelBufferRef from CGImage so here is the method:

- (CVPixelBufferRef) pixelBufferFromCGImage: (CGImageRef) image
{
    CGImageRetain(image);
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
                             [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
                             nil];
    CVPixelBufferRef pxbuffer = NULL;

    CVPixelBufferCreate(kCFAllocatorDefault, CGImageGetWidth(image),
                        CGImageGetHeight(image), kCVPixelFormatType_32ARGB, (CFDictionaryRef) options,
                        &pxbuffer);

    CVPixelBufferLockBaseAddress(pxbuffer, 0);
    void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pxdata, CGImageGetWidth(image),
                                                 CGImageGetHeight(image), 8, 4*CGImageGetWidth(image), rgbColorSpace,
                                                 kCGImageAlphaNoneSkipFirst);
    CGContextRetain(context);
    CGContextConcatCTM(context, CGAffineTransformMakeRotation(0));
    CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),
                                               CGImageGetHeight(image)), image);
    CGColorSpaceRelease(rgbColorSpace);
    CGContextRelease(context);
    CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
    CGContextRelease(context);
    CGImageRelease(image);
    return pxbuffer;
}

I call this method very often, it is for generating video frames, 25fps. This works fine most of the time, but at some point it crashes on this line of code:

 CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),
                                                   CGImageGetHeight(image)), image);

I`ve tested memory usage and memory leaks and everything seams fine, but the crush stil occurs.

Crash stack: Thread 6 Crashed:

0   ImageIO                         0x39270806 CGImageReadGetBytesAtOffset + 34
1   ImageIO                         0x392707d6 CGImageReadSessionGetBytes + 22
2   ImageIO                         0x39280c3c fill_input_buffer + 148
3   ImageIO                         0x3928003a read_markers + 154
4   ImageIO                         0x3927fd82 consume_markers + 34
5   ImageIO                         0x3927fbd2 _cg_jpeg_consume_input + 66
6   ImageIO                         0x3927fb62 _cg_jpeg_read_header + 38
7   ImageIO                         0x39292782 copyImageBlockSetJPEG + 2346
8   ImageIO                         0x3928953e ImageProviderCopyImageBlockSetCallback + 510
9   CoreGraphics                    0x38e0b9d6 CGImageProviderCopyImageBlockSetWithOptions + 158
10  CoreGraphics                    0x38e0b66a img_blocks_create + 370
11  CoreGraphics                    0x38e07a98 img_data_lock + 1488
12  CoreGraphics                    0x38e06d2a CGSImageDataLock + 126
1

1 Answer 1

1

From the crash stack, it looks like your image itself is released on some other thread, probably prior to your call to CGImageRetain(). The fact that you're calling CGImageRetain() here suggests that you expect that. But if that's the case, the retain is insufficient, since it creates a race condition. The image may have been destroyed before you try to retain it. So either the CGImageRetain is unnecessary or it is insufficient. In neither case should it be here (nor its balancing CGImageRelease).

This suggests very dangerous use of this image on some other thread, which either suggests that you're calling this method oddly, or that you've let your threading get over-complicated. For instance, if you're using NSThread directly, you've probably let it get too complicated.

I would audit your other retains and releases on this image and make sure that they cannot happen in the middle of calling this method. I'm pretty sure that's what's happening.

A side note: you have a redundant CGContextRetain/CGContextRelease that are not needed. When you called CGBitmapContextCreate(), that included a retain (it has "Create" in the name). That said, this is just a minor performance problem; you've properly balanced them. But it suggests that your memory management elsewhere may also be odd, and so you should audit it.

2
  • Following your advice, i've tried to make copy of image: - (CVPixelBufferRef) pixelBufferFromCGImage: (CGImageRef) imageInput { CGImageRef image = CGImageCreateCopy(imageInput); . . But the crash is still there. If the problem was Threading, this needed to be solution?
    – Ivan Alek
    Commented Dec 8, 2012 at 9:33
  • Making a copy in the first line doesn't change anything about the race condition (it is memory-wise identical to retaining). It sounds like you're calling this method at a point that you haven't retained the image. Perhaps you've called image.CGImage and then released image. Perhaps you've used a CGImage that you were passed from somewhere else, and then used it on another thread without retaining it. But you're going to have to fix that before you switch threads. The problem is almost certainly outside this method. These errors are most common when people manually thread with NSThread.
    – Rob Napier
    Commented Dec 8, 2012 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.