Skip navigation.
 
mlRe: Understanding reference count ownership
FROM : Christiaan Hofman
DATE : Wed May 07 23:47:05 2008

On 7 May 2008, at 11:32 PM, Gregory Seidman wrote:

> I'm coming across this in iPhone development, but it is just Cocoa and
> comes up in any Objective-C context without garbage collection.
>
> I think I understand the "Create Rule" where any time you alloc or 
> call a
> method with "create" in the name you are responsible for one reference
> count. What I'm less clear on is various factory-like methods. For 
> example:
>
> NSString *foo = [NSString stringWithFormat:@"%d times", 6];
>
> Does the calling code now own a reference count of foo? How about:
>
> NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL 
> urlWithString:@"http://google.com"]];
>
> Did the NSURL object just leak? For that matter:
>
> NSString *bar = @"bar";
>
> Does bar now have a reference count I have to decrement before it 
> leaves
> scope?
>
> I'm just a little unsure of what is handled for me and what I have to
> handle manually. I've already been bitten by autoreleasing stuff 
> that I
> thought I owned, so I'm erring on the side of leaking memory. Of 
> course,
> that's a bad plan for any app and particularly on the iPhone.
>
> --Greg



No. The rule is that only methods containing "create", "copy", and 
"alloc" in their name implicitly retain. Class factory methods return 
an autoreleased object, which effectively has retain count 0. Think of 
autorelease as release, so effectively it reduces the retain count by 
1. It just releases delayed. Similarly for constant strings like 
@"bar" (these are just static strings that stay around, but are not 
explicitly retained).

Christiaan

Related mailsAuthorDate
mlUnderstanding reference count ownership Gregory Seidman May 7, 23:32
mlRe: Understanding reference count ownership Christiaan Hofman May 7, 23:47
mlRe: Understanding reference count ownership j o a r May 7, 23:47
mlRe: Understanding reference count ownership Jim Correia May 7, 23:49
mlRe: Understanding reference count ownership mmalc Crawford May 8, 01:34
mlRe: Understanding reference count ownership Paul Sargent May 8, 15:16
mlRe: Understanding reference count ownership Christiaan Hofman May 8, 15:32
mlRe: Understanding reference count ownership mmalc Crawford May 8, 16:55
mlRe: Understanding reference count ownership Christiaan Hofman May 8, 17:16
mlRe: Understanding reference count ownership j o a r May 8, 17:30
mlRe: Understanding reference count ownership Sherm Pendley May 8, 17:50
mlRe: Understanding reference count ownership Christiaan Hofman May 8, 18:09
mlRe: Understanding reference count ownership Sherm Pendley May 8, 18:28
mlRe: Understanding reference count ownership Christiaan Hofman May 8, 18:48
mlRe: Understanding reference count ownership Sherm Pendley May 8, 19:16
mlRe: Understanding reference count ownership Christiaan Hofman May 10, 17:20
mlRe: Understanding reference count ownership Sherm Pendley May 10, 19:58
mlRe: Understanding reference count ownership Clark Cox May 10, 20:36