title: CIImage drawing EXC_BAD_ACCESS
body: <p>So, I have a <code>CIImage</code> that I'm attempting to draw in an <code>NSView</code>'s <code>-drawRect</code> method.</p>
  
  <p>This is the line of code that I call to draw the image:</p>
  
  <pre><code>[outputCoreImage drawInRect: [self bounds]
                     fromRect: originalBounds
                    operation: NSCompositeSourceOver
                     fraction: 1];
  </code></pre>
  
  <p><code>outputCoreImage</code>, <code>originalBounds</code>, and <code>[self bounds]</code> are all non-<code>nil</code>, 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 <code>EXC_BAD_ACCESS</code> on this line. If I walk up the stack, I find that the internal function call that breaks is on <code>CGLGetPixelFormat</code>.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I have zombies, guard malloc, and log exceptions all turned on, and none of them return any useful information.</p>
  
  <p><strong>Other OpenGL Testing:</strong></p>
  
  <p>I added this chunk:</p>
  
  <pre><code>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];
  </code></pre>
  
  <p>to the <code>-windowDidLoad</code> method of my <code>NSWindowController</code>. It runs without issue.</p>
  
  <p>I added this chunk:</p>
  
  <pre><code>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];
  </code></pre>
  
  <p>to another <code>NSView</code>'s <code>-drawRect</code>. I received the same error on <code>CGLGetPixelFormat</code>.</p>
  
  <p><strong>Radar:</strong></p>
  
  <p>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.</p>
  
  <p><strong>Workaround:</strong></p>
  
  <pre><code>CGContextRef contextReference = [[NSGraphicsContext currentContext] graphicsPort];
  CIContext *coreImageContext = [CIContext contextWithCGContext: contextReference
                                                        options: @{kCIContextUseSoftwareRenderer : @YES}];
  
  [coreImageContext drawImage: outputCoreImage
                       inRect: [self bounds]
                     fromRect: originalBounds];
  </code></pre>
  
  <p>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.</p>