Make CVDisplayLink + Automatic Reference Counting play well together

天大地大妈咪最大 提交于 2019-12-06 08:31:33

问题


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
}


来源:https://stackoverflow.com/questions/8835956/make-cvdisplaylink-automatic-reference-counting-play-well-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!