gpt4 book ai didi

objective-c - 让 CVDisplayLink + Automatic Reference Counting 一起玩

转载 作者:搜寻专家 更新时间:2023-10-30 20:00:56 24 4
gpt4 key购买 nike

我最近从使用 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;
}

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

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

所以我不能在回调中使用 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

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

我错过了什么吗?

最佳答案

用新的 @autoreleasepool block 包围代码:

@autoreleasepool {
// your c callback code here
}

关于objective-c - 让 CVDisplayLink + Automatic Reference Counting 一起玩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8835956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com