我是新手,正在尝试将arc应用到我的项目中。我想要了解__bridge及其相关内容,以便在将CGImageRef添加到容器时正确地进行类型转换。
某一行代码上出现了“Potential leak of an object stored…”的警告。以下是我的代码循环的基本过程:
CGImageRef renderedRef = CGBitmapContextCreateImage(UIGraphicsGetCurrentContext());
[_array addObject: (__bridge_transfer id)renderedRef];//_array is an iVar
在之后的某个阶段,我会执行以下操作:
CGImageRef iRef = (__bridge_retained CGImageRef)array[0];
//then I do something fancy with iRef
//at the end of the method, I get "Potential leak of an object stored…"
//I have no idea what to do
//I've tried CGImageRelease(iRef); but it still doesn't change the warning.
能有人解释一下吗?我尝试使用__bridge,但没有任何区别。
编辑1:
我扩展了分析器结果并跟踪了发生的情况。这是因为我在一个方法中使用iRef,例如:[self doSomethingFancy:iRef];在那个方法中,iRef被保留但没有被释放。所以这修复了警告,但我仍然有点困惑。
我不太清楚何时使用各种__bridge转换。在ARC下,以下代码会增加引用计数吗?
CGImageRef iRef = (__bridge CGImageRef)array[0];
同时,如果我告诉我的_array iVar使用removeAllObjects方法,是否会正确地减少它们的引用计数?
CGImageRelease(iRef)
是正确的。如果您仍然收到警告,请单击蓝色图标以获取有关分析器认为的详细报告。 - Martin R