Ray's playground

 

Memory Management(Chapter 3 of iOS Programming: The Big Nerd Ranch Guide)

If you create an object using a method whose name starts with alloc or new or contains copy, then you have taken ownership of it. (That is, assume that the new object has a retain count of 1 and is not in the autorelease pool.) You have a responsibility to release the object when you no longer need it. Here are some of the common methods that convey ownership: alloc (which is always followed by an init method), copy, and mutableCopy.
An object created through any other means – like a convenience method – is not owned by you. (That is, assume it has a retain count of one and is already in the autorelease pool, and thus doomed unless it is retained before the autorelease pool is drained.)
If you don’t own an object and you want to ensure its continued existence, take ownership by sending it the message retain. (This increments the retain count.) When you own an object and no longer need it, send it the message release or autorelease.
(release decrements the retain count immediately. autorelease causes the message release to get sent when the autorelease pool is drained.)
As long as an object has at least one owner, it will continue to exist. (When its retain count goes to zero, it is sent the message dealloc.) 

posted on 2011-10-31 17:54  Ray Z  阅读(284)  评论(0编辑  收藏  举报

导航