Core Foundation objects and retain/release messages

不问归期 提交于 2019-12-05 07:00:19

问题


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];

Since arrays retain their contents, color receives a retain message (not CFRetain(), right?). What happens in this case from memory management point of view?


回答1:


From Core Foundation Design Concepts:

Note from the example that the memory management functions and methods are also interchangeable—you can use CFRelease with a Cocoa object and release and autorelease with a Core Foundation object.

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.

The result is the same: retain does the same as CFRetain, release does the same as CFRelease, etc.

A few things to be aware of:

  • 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 the CFAutorelease function.) If you're not using ARC, then, as mentioned in the documentation quoted above, you can send autorelease 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 on NULL (you'll crash). Quartz has some class-specific functions, such as CGContextRetain/Release, that include a NULL check; whether you want to use those or always do your own NULL checks is a matter of style.
  • CF's retain and release functions work under garbage collection, whereas retain and release messages are no-ops (as if sent to nil). Doesn't matter unless you're working on a GC'd Mac app, in which case you will need to use CFRetain and CFRelease on CF objects.
  • Similarly, under ARC, retain and release messages will be illegal and CF objects won't be automatically reference-counted. You will need to use CFRetain and CFRelease 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 and release messages).


来源:https://stackoverflow.com/questions/7589384/core-foundation-objects-and-retain-release-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!