Use autorelease to Send a Deferred release---使用autorelease发送一个延期释放的消息

You use autorelease when you need to send a deferredrelease message—typically when returning an object from a method. For example, you could implement thefullName method like this:

当你需要发送一个延期释放消息的时候,你可以使用autorelease----当作为方法返回值的时候,就是一个典型的例子。例如:你可以像下面这样实现一个fullName的方法:

- (NSString *)fullName {


    NSString *string = [[[NSString alloc] initWithFormat:@"%@ %@",


                                          self.firstName, self.lastName] autorelease];


    return string;


}


You own the string returned by alloc. To abide by the memory management rules, you must relinquish ownership of the string before you lose the reference to it. If you userelease, however, the string will be deallocated before it is returned (and the method would return an invalid object). Usingautorelease, you signify that you want to relinquish ownership, but you allow the caller of the method to use the returned string before it is deallocated.

你string保留一个通过alloc创建的对象的所有权。要遵守内存管理规则,在丢失对string字符串的引用之前,你必须断开string的所有权。如果使用release,无论怎样,string对象在会在返回之前释放掉(但是方法又不能返回一个非法的对象)。使用autorelease,你表示你想要断开连接,但是你允许在对象释放之前,调用这个方法的对象使用他。



You could also implement the fullName method like this:

你也可以这样实现fullName方法

- (NSString *)fullName {


    NSString *string = [NSString stringWithFormat:@"%@ %@",


                                 self.firstName, self.lastName];


    return string;


}


Following the basic rules, you don’t own the string returned by stringWithFormat:, so you can safely return the string from the method.

By way of contrast, the following implementation is wrong:

- (NSString *)fullName {


    NSString *string = [[NSString alloc] initWithFormat:@"%@ %@",


                                         self.firstName, self.lastName];


    return string;


}


According to the naming convention, there is nothing to denote that the caller of thefullName method owns the returned string. The caller therefore has no reason to release the returned string, and it will thus be leaked.


You Don’t Own Objects Returned by Reference


Some methods in Cocoa specify that an object is returned by reference (that is, they take an argument of typeClassName ** or id *). A common pattern is to use anNSError object that contains information about an error if one occurs, as illustrated byinitWithContentsOfURL:options:error: (NSData) andinitWithContentsOfFile:encoding:error: (NSString).

In these cases, the same rules apply as have already been described. When you invoke any of these methods, you do not create theNSError object, so you do not own it. There is therefore no need to release it, as illustrated in this example:

NSString *fileName = <#Get a file name#>;


NSError *error;


NSString *string = [[NSString alloc] initWithContentsOfFile:fileName


                        encoding:NSUTF8StringEncoding error:&error];


if (string == nil) {


    // Deal with error...


}


// ...


[string release];




你可能感兴趣的:(Use autorelease to Send a Deferred release---使用autorelease发送一个延期释放的消息)