Skip navigation.
 
mlRe: Understanding reference count ownership
FROM : Jim Correia
DATE : Wed May 07 23:49:52 2008

On May 7, 2008, at 5: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.


The documentation is complete. It lists all the situations in which 
you are responsible for releasing an object when you are done with it. 
In all other situations, it is not your responsibility to release it.

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


Those fall under the "not your responsibility to release the object" 
cases.

The objects will typically be autoreleased by the "factory" method, 
but that's an implementation detail you don't need to worry about. 
(The objects could have come from some magical object forest, for all 
you care.) The only thing you need to know is should I release this 
when I'm done with it, and/or do I need to retain this if I want to 
hold onto it for awhile.

Jim

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