2

I recently switched from using NSTimer to CVDisplayLink to redraw my OpenGL animation, but i've got a little problem making it work with ARC switched on:

/*
 * This is the renderer output callback function.
 */
static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* displayLinkContext)
{
  // now is the time to render the scene
  [((__bridge BDOpenGLView*)displayLinkContext) setNeedsDisplay:YES];
  // always succeeds, because we don't actually do anything here anyway
  return kCVReturnSuccess;
}

The display link callback function has to be written in C, to be used as a parameter for

// set the renderer output callback function
CVDisplayLinkSetOutputCallback(displayLink, &displayLinkCallback, (__bridge void*)self);

So i can't use self within in the callback, but using ((__bridge BDOpenGLView*) displayLinkContext) produces a memory leak:

objc[29390]: Object 0x1001b01f0 of class NSConcreteMapTable autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug

I read, that i have to set up an NSAutoreleasePool myself, but i can't with ARC switched on.

Am i missing something?

cargath
  • 832
  • 8
  • 18

1 Answers1

5

Surround the code with the new @autoreleasepool block:

@autoreleasepool {
  // your c callback code here
}
Jason Coco
  • 77,985
  • 20
  • 184
  • 180
  • I hit this same exact thing when I was migrating some GC code over to ARC a month ago. I got so used to blocks and the fact that [they don't need explicit autorelease pools](http://stackoverflow.com/questions/4141123/do-you-need-to-create-an-nsautoreleasepool-within-a-block-in-gcd) that I forgot about creating one for the background thread that CVDisplayLink uses with its callbacks. – Brad Larson Jan 13 '12 at 15:45