1

当您像这样创建CGLayer,然后获取上下文时……似乎不可能释放 CGContextRef

释放 CGLayerRef 本身(显然)工作正常。

你会认为你可以在发布 CGLayer 之前发布 CGContextRef - 但不是吗?也不能在释放 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???

关于乔尔的精彩回答如下:

释放CGLayerRef是否正确和正确?Joel 指出:
“是的,因为您从中获取它的函数的签名中包含‘Create’。请参阅:documentation/CoreFoundation/”

4

1 回答 1

5

您不拥有从 CGLayerGetContext 返回的上下文,因此不应释放它*。特别是,请参阅http://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html#//apple_ref/doc/writerid/cfGetRule了解有关“获取”功能核心基金会。

*:至少,鉴于您的示例代码,您不应该发布它。如果你先保留它(CGContextRetain(weird)),那么你应该有一个 CGContextRelease 来平衡它。

于 2011-01-04T12:56:40.617 回答