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






Cocoa mail archive

