Core Foundation对象并保留/释放消息 [英] Core Foundation objects and retain/release messages
问题描述
假设我们有一些Core Foundation对象,例如 CGColorRef
,它们被添加到 NSArray
:
Let's say we have some Core Foundation objects, such as CGColorRef
, that are added to an NSArray
like this:
CGColorRef color = ...;
NSArray *array = [NSArray arrayWithObject:(id)color];
由于数组保留其内容, color
a retain
消息(不是 CFRetain()
,对吗?在这种情况下,从内存管理的角度来看会发生什么?
Since arrays retain their contents, color
receives a retain
message (not CFRetain()
, right?). What happens in this case from memory management point of view?
推荐答案
从示例中注意,内存管理函数和方法也可以互换 - 您可以使用 CFRelease
与Cocoa对象和发布
Note from the example that the memory management functions and methods are also interchangeable—you can use
CFRelease
with a Cocoa object andrelease
andautorelease
with a Core Foundation object.
它没有特别提到 retain
,但是,在实践中,也可以, copy
(各种类 CFFooCreateCopy
)和描述
( CFCopyDescription
) 。最后一个是当使用 NSLog
和其他字符串时,如何传递CF对象作为%@
格式化规范的值格式化函数和方法。
It doesn't specifically mention retain
, but, in practice, that works as well, as do copy
(various classes' CFFooCreateCopy
) and description
(CFCopyDescription
). The last is how you can pass CF objects as the value for a %@
formatting specification when using NSLog
and other string-formatting functions and methods.
结果是相同的: retain
与 CFRetain
, release
与 CFRelease
等同。
The result is the same: retain
does the same as CFRetain
, release
does the same as CFRelease
, etc.
有几件事要注意:
- 在iOS 7和OS X 10.9之前,是没有CF对应函数NSObject的
autorelease
方法。 (7和10.9带来了CFAutorelease
函数。)如果你不使用ARC,那么,如上面引用的文档中提到的,你可以发送 - 您可以向
nil $>发送消息 c $ c>,但是你不能在
NULL
上调用CF函数(你会崩溃)。 Quartz有一些类特定的函数,如CGContextRetain
/Release
,包括NULL
检查;无论是要使用这些还是总是自己执行NULL
检查都是一种风格问题。 - CF的保留和释放功能垃圾收集,而
保留
和发布
消息是无操作(如果发送到CFRetain
和CFRelease $ c $ c> c $ c> c $ c>
code> $ c>消息将是非法的,CF对象不会被自动引用计数。您需要在CF对象上使用 - 总是做正确的事情,如记录。通常,这意味着强大的参考。在GC之外,这意味着集合(数组,字典等)将保留和释放其对象,无论是自动(通过分配,如果其代码被ARCified)或手动(通过显式
保留
和release
消息)。
CFRetain
和 CFRelease
。 - Prior to iOS 7 and OS X 10.9, there is no CF counterpart function to NSObject's
autorelease
method. (7 and 10.9 brought theCFAutorelease
function.) If you're not using ARC, then, as mentioned in the documentation quoted above, you can sendautorelease
to a CF object, and it works the same as on an NSObject. - You can send a message to
nil
, but you can't call CF functions onNULL
(you'll crash). Quartz has some class-specific functions, such asCGContextRetain
/Release
, that include aNULL
check; whether you want to use those or always do your ownNULL
checks is a matter of style. - CF's retain and release functions work under garbage collection, whereas
retain
andrelease
messages are no-ops (as if sent tonil
). Doesn't matter unless you're working on a GC'd Mac app, in which case you will need to useCFRetain
andCFRelease
on CF objects. - Similarly, under ARC,
retain
andrelease
messages will be illegal and CF objects won't be automatically reference-counted. You will need to useCFRetain
andCFRelease
on CF objects. - Collections always do the right thing, as documented. Usually, this means a strong reference. Outside of GC, that means the collection (array, dictionary, etc.) will retain and release its objects, whether automatically (by assignments, if its code is ARCified) or manually (by explicit
retain
andrelease
messages).
这篇关于Core Foundation对象并保留/释放消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!