CIImage drawing EXC_BAD_ACCESS

2019-05-28 20:53发布

问题:

So, I have a CIImage that I'm attempting to draw in an NSView's -drawRect method.

This is the line of code that I call to draw the image:

[outputCoreImage drawInRect: [self bounds]
                   fromRect: originalBounds
                  operation: NSCompositeSourceOver
                   fraction: 1];

outputCoreImage, originalBounds, and [self bounds] are all non-nil, and indeed are their respective expected values. On Lion (OS X 10.7), this worked fine, however on Mountain Lion (OS X 10.8) I receive an EXC_BAD_ACCESS on this line. If I walk up the stack, I find that the internal function call that breaks is on CGLGetPixelFormat.

frame #0: 0x00007fff99c3b26d OpenGL`CGLGetPixelFormat + 27
frame #1: 0x00007fff8fa98978 CoreImage`-[CIOpenGLContextImpl createAccelContext] + 335
frame #2: 0x00007fff8fa98d30 CoreImage`-[CIOpenGLContextImpl updateContext] + 111
frame #3: 0x00007fff8fa9a865 CoreImage`-[CIOpenGLContextImpl _lockfeContext] + 25
frame #4: 0x00007fff8fa7e4ac CoreImage`-[CIContextImpl setObject:forKey:] + 120
frame #5: 0x00007fff8fa9881c CoreImage`-[CIOpenGLContextImpl setObject:forKey:] + 259
frame #6: 0x00007fff90db93e3 libCGXCoreImage.A.dylib`cgxcoreimage_instance_render + 1477
frame #7: 0x00007fff97074ac6 CoreGraphics`CGSCoreImageInstanceRender + 32
frame #8: 0x00007fff947c89cc libRIP.A.dylib`ripc_AcquireCoreImage + 1344
frame #9: 0x00007fff947b8a00 libRIP.A.dylib`ripc_DrawShading + 9680
frame #10: 0x00007fff96cb2b2b CoreGraphics`CGContextDrawShading + 51
frame #11: 0x00007fff8fa78237 CoreImage`-[CICGContextImpl render:] + 918
frame #12: 0x00007fff8fa7d76a CoreImage`-[CIContext drawImage:inRect:fromRect:] + 1855
frame #13: 0x00007fff9897b8f3 AppKit`-[CIImage(NSAppKitAdditions) drawInRect:fromRect:operation:fraction:] + 243

I have zombies, guard malloc, and log exceptions all turned on, and none of them return any useful information.

Other OpenGL Testing:

I added this chunk:

CIImage *anImage  = [[CIImage alloc] init];
[[[self window] contentView] lockFocus];
{
    [anImage drawInRect: [[self window] frame]
               fromRect: [[self window] frame]
              operation: NSCompositeSourceOver
               fraction: 1];
}
[[[self window] contentView] unlockFocus];

to the -windowDidLoad method of my NSWindowController. It runs without issue.

I added this chunk:

NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString: @"http://cache.virtualtourist.com/14/684723-Red_Square_Moscow.jpg"]];

CIImage *anImage  = [[CIImage alloc] initWithData: data];
[self lockFocus];
{
    [anImage drawInRect: [self bounds]
               fromRect: [anImage extent]
              operation: NSCompositeSourceOver
               fraction: 1];
}
[self unlockFocus];

to another NSView's -drawRect. I received the same error on CGLGetPixelFormat.

Radar:

The folks over at #macdev seem to believe that this is in an OS issue, and I'm not inclined to disagree. I've filed a radar (rdar://11980704) explaining the problem and linking back to this question.

Workaround:

CGContextRef contextReference = [[NSGraphicsContext currentContext] graphicsPort];
CIContext *coreImageContext = [CIContext contextWithCGContext: contextReference
                                                      options: @{kCIContextUseSoftwareRenderer : @YES}];

[coreImageContext drawImage: outputCoreImage
                     inRect: [self bounds]
                   fromRect: originalBounds];

It looks like forcing the image to draw using the software renderer "solves" the problem. It's a bit blurry and slow, but it doesn't crash.

回答1:

Resolved in 10.8.1

The folks over at #macdev seem to believe that this is in an OS issue, and I'm not inclined to disagree. I've filed a radar (rdar://11980704) explaining the problem and linking back to this question.



回答2:

This might be the first time your app calls into any OpenGL routine which is probably doing some first-run initialization or checks.

For an experiment, try adding a little OpenGL code earlier in your app and see if that crashes as well. Try to think about why calling OpenGL might causing a crash.