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
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






Cocoa mail archive

