Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 无法释放CGContextRef(上下文来自CGLayer)_Iphone_Cocoa_Cgcontext_Cglayer - Fatal编程技术网

Iphone 无法释放CGContextRef(上下文来自CGLayer)

Iphone 无法释放CGContextRef(上下文来自CGLayer),iphone,cocoa,cgcontext,cglayer,Iphone,Cocoa,Cgcontext,Cglayer,当您像这样创建一个CGLayer并获取上下文时,似乎不可能释放CGContextRef 释放CGLayerRef本身(显然)效果很好 您可能认为您可以在发布CGLayer之前发布cgcContextRef,但不是吗?也不能仅在释放CGLayer之后释放CGContextRef 如果释放CGContextRef,应用程序将崩溃 CGLayerRef aether = CGLayerCreateWithContext( UIGraphicsGetCurrentContext(), CGSiz

当您像这样创建一个CGLayer并获取上下文时,似乎不可能释放CGContextRef

释放CGLayerRef本身(显然)效果很好

您可能认为您可以在发布CGLayer之前发布cgcContextRef,但不是吗?也不能仅在释放CGLayer之后释放CGContextRef

如果释放CGContextRef,应用程序将崩溃

CGLayerRef aether = CGLayerCreateWithContext(
    UIGraphicsGetCurrentContext(), CGSizeMake(1024,768), NULL);
CGContextRef weird = CGLayerGetContext(aether);

// paths, strokes, filling etc
// paths, strokes, filling etc

// try releasing weird here
CGLayerRelease(aether);
// or, try releasing weird here
有人知道这里发生了什么吗?(进一步注意,CGContextRelease实际上与CFRelease相同,只是进行了一些nil检查。)

事实上,您应该永远不要手动释放CGContextRef吗?有人知道吗?干杯

CGContextRelease(weird); // impossible, not necessary, doesn't work???

关于Joel的精彩回答,如下所示:

释放CGLayerRef是否正确和适当?乔尔指出:

“是的,因为您从中获取它的函数在其签名中有‘Create’。请参阅:documentation/CoreFoundation/”

您不拥有从CGLayerGetContext返回的上下文,因此不应释放它*。特别是,关于核心基础中的“获取”功能的信息,请参阅


*:至少,考虑到示例代码,您不应该发布它。如果您先保留它(CGContextRetain(怪异)),那么您应该有一个CGContextRelease来平衡它。

您不拥有从CGLayerGetContext返回的上下文,因此不应该释放它*。特别是,关于核心基础中的“获取”功能的信息,请参阅


*:至少,考虑到示例代码,您不应该发布它。如果您先保留它(CGContextRetain(怪异)),那么您应该有一个CGContextRelease来平衡它。

是的,因为您获取它的函数的签名中有“Create”。请参见“是”,因为您从中获取它的函数的签名中有“Create”。看见