本文介绍了使CVDisplayLink +自动参考计数一起播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从使用NSTimer切换到CVDisplayLink重新绘制我的OpenGL动画,但我有一个小问题,使其工作与ARC打开:

  / * 
*这是渲染器输出回调函数。
* /
static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink,const CVTimeStamp * now,const CVTimeStamp * outputTime,CVOptionFlags flagsIn,CVOptionFlags * flagsOut,void * displayLinkContext)
{
// now是渲染场景的时间
[((__bridge BDOpenGLView *)displayLinkContext)setNeedsDisplay:YES];
//总是成功,因为我们实际上不做任何事情
return kCVReturnSuccess;
}

显示链接回调函数必须用C语言编写,



的参数

  //设置渲染器输出回调函数
CVDisplayLinkSetOutputCallback(displayLink,& displayLinkCallback, *)自);

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

  objc [29390]:NSConcreteMapTable类的对象0x1001b01f0自动释放,没有池 - 正好泄露 - 在objc_autoreleaseNoPool()上断开以调试

我读到,我必须设置一个 NSAutoreleasePool 自己,但我不能与ARC打开。 p>

我错过了什么?

解决方案

code> @autoreleasepool 封锁:

  @autoreleasepool {
// c回调代码here
}


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?

解决方案

Surround the code with the new @autoreleasepool block:

@autoreleasepool {
  // your c callback code here
}

这篇关于使CVDisplayLink +自动参考计数一起播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 04:27