内存管理法则

Basic Memory Management Rules
基本内存管理原则

The memory management model is based on object ownership. Any object may have one or more owners. As long as an object has at least one owner, it continues to exist. If an object has no owners, the runtime system destroys it automatically. To make sure it is clear when you own an object and when you do not, Cocoa sets the following policy:

You own any object you create
你会拥有任何自己创建的对象
You create an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy).
使用名为“alloc”、“new”、“copy”或者“mutableCopy”(比如,alloc,newObject,or mutableCopy)创建一个对象

You can take ownership of an object using retain
你会接管你对它使用retain的对象
A received object is normally guaranteed to remain valid within the method it was received in, and that method may also safely return the object to its invoker. You use retain in two situations: (1) In the implementation of an accessor method or an init method, to take ownership of an object you want to store as a property value; and (2) To prevent an object from being invalidated as a side-effect of some other operation (as explained in “Avoid Causing Deallocation of Objects You’re Using”).

When you no longer need it, you must relinquish ownership of an object you own
当你不需要这个对象的时候,你必须放弃这段从属关系
You relinquish ownership of an object by sending it a release message or an autorelease message. In Cocoa terminology, relinquishing ownership of an object is therefore typically referred to as “releasing” an object.
使用release或者autorelease放弃关系

You must not relinquish ownership of an object you do not own
千万不要放弃与自己无关的从属关系
This is just corollary of the previous policy rules, stated explicitly.


Ownership Policy Is Implemented Using Retain Counts

The ownership policy is implemented through reference counting—typically called “retain count” after the retain method. Each object has a retain count.

When you create an object, it has a retain count of 1.
创建对象的时候,retain count为1

When you send an object a retain message, its retain count is incremented by 1.
retain一个对象的时候,该对象的retain count 加1

When you send an object a release message, its retain count is decremented by 1.
release一个对象的时候,该对象的retain count减1

When you send an object a autorelease message, its retain count is decremented by 1 at the end of the current autorelease pool block.
autorelease一个对象的时候,该对象的retain count从当前autorelease pool block的末尾减1

If an object’s retain count is reduced to zero, it is deallocated.
当一个对象的retain count减至零,它会被dealloc

Important: There should be no reason to explicitly ask an object what its retain count is (see retainCount). The result is often misleading, as you may be unaware of what framework objects have retained an object in which you are interested. In debugging memory management issues, you should be concerned only with ensuring that your code adheres to the ownership rules.

Use Accessor Methods to Make Memory Management Easier
Use Accessor Methods to Set Property Values
Suppose you want to implement a method to reset the counter. You have a couple of choices. The first implementation creates the NSNumber instance with alloc, so you balance that with a release.

Don’t Use Accessor Methods in Initializer Methods and dealloc
The only places you shouldn’t use accessor methods to set an instance variable are in initializer methods and dealloc. To initialize a counter object with a number object representing zero, you might implement an init method

Avoid Causing Deallocation of Objects You’re Using
1. When an object is removed from one of the fundamental collection classes.
When an object is removed from one of the fundamental collection classes, it is sent a release (rather than autorelease) message. If the collection was the only owner of the removed object, the removed object (heisenObject in the example ) is then immediately deallocated.

2. When a “parent object” is deallocated.
In some situations you retrieve an object from another object, and then directly or indirectly release the parent object. If releasing the parent causes it to be deallocated, and the parent was the only owner of the child, then the child (heisenObject in the example) will be deallocated at the same time (assuming that it is sent a release rather than an autorelease message in the parent’s dealloc method).


算了。。。写了这么多白写了。。。看官方的内存管理指南就可以了。。多看才有用。。









猜你喜欢

转载自zl4393753.iteye.com/blog/1728470
今日推荐