开发者

Retain/Release through intermediary method

I think I understand retain/release in objective-C for the most part. However, I have a specific case I am unsure about. Here is an example:

开发者_JAVA百科+ (NSString *)getPlayerNameByIndex:(NSInteger)globalIndex:(ABAddressBookRef)addressBook
{
    ...
    Player *player = [PlayerHelper loadPlayer:globalIndex];
    NSString *name = [PlayerHelper getPlayerName:player :addressBook];
    [player release];

    // 'retain' here?    
    return name;
}

+ (NSString *)getPlayerName:(Player *)player:(ABAddressBookRef)addressBook
{
    ...
    NSString *name = [[[NSString alloc] initWithString:player.nickname] autorelease];
    return name;
}

So then I call...

NSString *name = [PlayerHelper getPlayerNameByIndex:index:addressBook];
// name is 'autorelease'?

What I saw on random occasions is that the view sometimes shows the 'name' field as empty when it populates the table after coming back from another view. This could be another issue but I want to be sure of my use of 'autorelease'.

The core of my question is the use of 'autorelease' in getPlayerName. Does the 'autorelease' state of being get passed through method getPlayerNameByIndex to the caller?

Or, do I have to call 'retain' in the intermediary method? I am thinking 'autorelease' may be releasing in method getPlayerNameByIndex.

Hopefully my question is clear. Any help is appreciated.

Update: Some more info for clarification...

NSError *error = nil;
Player *player = nil;
NSArray *array = [appDelegate.managedObjectContext executeFetchRequest:request error:&error];
if ([array count] == 1)
{
    player = [array objectAtIndex:0];
    [player retain];
}

This is essentially the "loadPlayer" method which loads info from core data. From the answers it sounds like I do not need to call [player retain], since it is an autorelated object, and I can simply return "player" and use it? Thanks for the responses!


The core of my question is the use of 'autorelease' in getPlayerName. Does the 'autorelease' state of being get passed through method getPlayerNameByIndex to the caller?

The answer is yes.

Or, do I have to call 'retain' in the intermediary method?

whether you want to call retain depends on the semantics of your method.

In Obj-C/Cocoa, the following convention applies: a method whose name begins with “alloc” or “new” or contains “copy” will return a retained object; otherwise you can expect to get an autoreleased object, then it is the caller responsibility to retain it according to its needs.

I am thinking 'autorelease' may be releasing in method getPlayerNameByIndex.

autoreleased objects are released at the next point in time when the autorelease pool is drained; this is usually associated to going back to the main loop (though, no details are available about this); so you can be pretty sure that auto-releasing does not kick in in getPlayerNameByIndex...

Hope this helps clarifying the issue...


In getPlayerNameByIndex The line:

[player release];

is wong, remove it. You did not obtain ownership. Ownership is ob gained by calling a method with alloc or the method names starts with new, copy or an explicit retain. (NARC).

You do not need to release player because you did not obtain ownership, see above rule.

In getPlayerName: can be simplified to:

return player.nickname;

The method name can be simplifies to:

+ (NSString *)getPlayerName:(Player *)player
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜