views:

57

answers:

1

Guys: Please help. I have a utility class to return a NSString with encoding. Like this:

/*Get a NSString with chinese encoding*/
+(NSString*) getGBKString:(void *)buffer
{
  NSString* string = [[[NSString alloc] 
                      initWithBytes:buffer 
                        length:sizeof(buffer) 
                        encoding:kCFStringEncodingGB_18030_2000] 
                      autorelease];

  return string;
}

Here the autorelease is the right thing to do?

If so, the method caller should call retain incase the NSString object is released?

+6  A: 

Here the autorelease is the right thing to do?

Yes. Since the method does not contain +alloc/new/copy, to follow the Cocoa memory management rules, you should return an object with no ownership.

Alternatively, you could rename the method to +newGBKString: and remove the -autorelease, then the people knowing the convention will know the caller will gain the ownership.

If so, the method caller should call retain incase the NSString object is released?

The caller should -retain if it wants to keep the NSString.

KennyTM
Thank Kenny and Jacob first. But I am confused a little. As you said, the -autorelease will change the retain count to 0, then the NSString instance should be dealloced before return, right? Then the caller will get a nil?
icespace
The retain count will drop to 0 only once the autorelease pool is drained.
Pierre Bernard
@icespace: `-autorelease` is basically "release later". When you call `-autorelease`, the ownership will be transferred to an auto-release pool which will `-release` it when run loop ticks. The effect is like returning a "dying" object (but still valid) of retain count 0.
KennyTM
@Kenny: How the auto-release pool work in iOS? I thought there is an implicit auto-release pool related with the method. Since the method is static method, the auto-release pool will release the method scope variables after the method is poped from the stack?
icespace
@icespace: See http://stackoverflow.com/questions/673372/when-does-autorelease-release-on-an-iphone-app.
KennyTM
@Kenny: Thanks a lot, it is really helpful and it is much quicker than google it.
icespace
-1 there are no objects with a retain count of 0. The first thing that release does is check if the retain count is currently 1 and dealloc the object if it is. -autorelease, in fact, does *nothing* to the retain count. **Forget about retain counts** they are an implementation detail. Think in terms of ownership. Autorelease transfers ownership of an object to the autorelease pool. That's all that it does.
JeremyP
NB, if you edit the post to remove the bit about retain count 0, I'll change the down vote to an up vote. Everything else in it is correct.
JeremyP
JeremyP: There's nothing in it about retain count = 0 now. Kenny: The fact that is a class method (there is no such thing as a “static method”) does not matter. Also, the call stack has nothing to do with the lifetimes of autorelease pools, just as it has nothing to do with the lifetimes of any other objects.
Peter Hosey