我正在为 iPhone 开发。我想通过 CGPathCreateMutable() 创建一个可变路径,并且我想从创建它的函数中返回它。我应该在完成后调用 CGPathRelease() 。但既然我要归还它,我希望自动释放它。由于 Quartz 路径是 C 代码(并且看起来不像目标 C 对象),所以我不能对其调用 autorelease 是否正确?
编辑:
对于其他偶然发现这个问题的人,以下建议适用于仅返回 Core 基础对象的 C 函数。对于返回 Core 基础对象的 Objective C 方法,请参阅与返回的 Quartz 对象有关的所有权
I am developing for iphone. I want to creating a mutable path via CGPathCreateMutable(), and I want to return it out of the function which creates it. I'm suppose to call a CGPathRelease() when I'm done with it. But since I'm returning it I wish to autorelease it. Since Quartz path is a C code (and doesn't look like an objective C object), is it correct that I cannot call autorelease on it?
Edit:
For others who stumble upon this question, the below advise is for C functions returning Core foundation objects only. For objective C methods returning Core foundation objects, see Ownership regarding to returned Quartz objects
发布评论
评论(1)
正确的。自动释放池存在于基础层及以上(AppKit/UIKit等)。 CoreFoundation/CoreGraphics 对象不存在它们。
解决这个问题的简单方法是重命名您的函数。如果您的函数当前被命名为:
那么您应该将其重命名为:
以便您可以按照 创建规则。
Correct. Autorelease pools exist in the Foundation layer and above (AppKit/UIKit, etc). They don't exist for CoreFoundation/CoreGraphics objects.
The simple way around this is to rename your function. If your function is currently named:
Then you should rename it to:
so that you are safe to return an object with a +1 retain count by following the Create rule.