Core Foundation对象并保留/释放消息 [英] Core Foundation objects and retain/release messages

查看:114
本文介绍了Core Foundation对象并保留/释放消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一些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?

推荐答案

从Core Foundation设计概念

从示例中注意,内存管理函数和方法也可以互换 - 您可以使用 CFRelease 与Cocoa对象和发布

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.

它没有特别提到 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的保留和释放功能垃圾收集,而保留发布消息是无操作(如果发送到 )。除非你正在使用GC'd Mac应用程序,在这种情况下,您需要使用 CFRetain CFRelease c $ c> c $ c> c $ c>
  • code> $ c>消息将是非法的,CF对象不会被自动引用计数。您需要在CF对象上使用 CFRetain CFRelease
  • 总是做正确的事情,如记录。通常,这意味着强大的参考。在GC之外,这意味着集合(数组,字典等)将保留和释放其对象,无论是自动(通过分配,如果其代码被ARCified)或手动(通过显式保留 release 消息)。

  • 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).

这篇关于Core Foundation对象并保留/释放消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆