使CVDisplayLink +自动参考计数可以很好地协同工作

car*_*ath 3 cocoa core-animation objective-c automatic-ref-counting

我最近从使用NSTimer切换到CVDisplayLink来重绘我的OpenGL动画,但是我有一个小问题,它可以在开启ARC时使用它:

/*
 * 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;
}
Run Code Online (Sandbox Code Playgroud)

显示链接回调函数必须用C编写,以用作参数

// set the renderer output callback function
CVDisplayLinkSetOutputCallback(displayLink, &displayLinkCallback, (__bridge void*)self);
Run Code Online (Sandbox Code Playgroud)

所以我不能self在回调中使用内部,但使用((__bridge BDOpenGLView*) displayLinkContext)会产生内存泄漏:

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

我读过,我必须自己设置NSAutoreleasePool,但我不能用ARC打开.

我错过了什么吗?

Jas*_*oco 6

使用新@autoreleasepool块包围代码:

@autoreleasepool {
  // your c callback code here
}
Run Code Online (Sandbox Code Playgroud)