Make CVDisplayLink + Automatic Reference Counting

2019-04-12 04:55发布

问题:

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?

回答1:

Surround the code with the new @autoreleasepool block:

@autoreleasepool {
  // your c callback code here
}