Quartz2D的内存管理

来源:互联网 发布:小猪cms最新版 编辑:程序博客网 时间:2024/05/28 06:07

Quartz uses the Core Foundation memory management model, in which objects are reference counted. When created, Core Foundation objects start out with a reference count of 1. You can increment the reference count by calling a function to retain the object, and decrement the reference count by calling a function to release the object. When the reference count is decremented to 0, the object is freed. This model allows objects to safely share references to other objects.

There are a few simple rules to keep in mind:

If you create or copy an object, you own it, and therefore you must release it. That is, in general, if you obtain an object from a function with the words “Create” or “Copy” in its name, you must release the object when you’re done with it. Otherwise, a memory leak results.
If you obtain an object from a function that does not contain the words “Create” or “Copy” in its name, you do not own a reference to the object, and you must not release it. The object will be released by its owner at some point in the future.
If you do not own an object and you need to keep it around, you must retain it and release it when you’re done with it. You use the Quartz 2D functions specific to an object to retain and release that object. For example, if you create a CGColorspace object, you use the functions CGColorSpaceRetain and CGColorSpaceRelease to retain and release the object as needed. You can also use the Core Foundation functions CFRetain and CFRelease, but you must be careful not to pass NULL to these functions.

Quartz使用的核心基础,内存管理模式,在其中的对象引用计数。创建时,核心基础对象的引用计数减1。您可以通过调用函数保留的对象,通过调用一个函数来释放该对象的引用计数递减递增引用计数。当引用计数递减到0时,对象被释放。这个模型允许对象安全地共享其他对象的引用。 

有几个简单的规则要牢记: 

,如果您创建或复制一个对象,你自己,因此,你必须释放它。也就是说,在一般情况下,如果你获得一个对象从一个函数的话,“创建”或“复制”,在它的名字,你必须释放的对象,当你用它做。否则,将导致内存泄漏。 
从一个不包含单词“创建”或在其名称中的“复制”的功能,如果你获得一个对象,你没有自己的对象的引用,你不能释放它。由它的主人,在一些点在未来将被释放的对象。 
如果你没有自己的一个对象,你需要保持周围,你必须保留它,释放它,当你用它做。您可以使用特定的对象保留和释放该对象的2D功能的石英。例如,如果您创建一个CGColorspace对象,您可以使用的功能CGColorSpaceRetain和CGColorSpaceRelease需要保留和释放对象。您还可以使用CFRetain和CFRelease核心基础功能,但你必须小心,不要传递NULL这些功能。 
原创粉丝点击