Thanks Clark. Cleared my doubts ;)

On Mon, Mar 30, 2009 at 4:38 AM, Clark Cox <clarkc...@gmail.com> wrote:

> On Sun, Mar 29, 2009 at 12:24 PM, Ignacio Enriquez <nach...@gmail.com>
> wrote:
> > Hi everyone.
> > I still have some doubts of  should I release or not? and the most
> > important reaons why should I or why shouldn't I do it. Any answer
> > would be very appreciated.
> >
> > Consider this:
> >
> > NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
> > NSArray *rawArray = [defaults objectForKey:@"lastArray"];
> > NSMutableArray *tempArray = [[NSMutableArray alloc] init];
> > for(int i= 0; i< [rawArray count]; i++){
> >   NSDictionary *dic = [rawArray objectAtIndex:i];
> >   Mail *mail = [[Mail alloc] initWithDictionary:dic];
> >   [tempArray addObject:mail];
> >   [mail release];
> > }
> > theArray = tempArray;
> >
> > Here theArray is a property:
> >
> > @property (nonatomic, retain) NSArray *theArray;
>
> From Cocoa's memory management rules
> <
> http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmObjectOwnership.html
> >:
>
> - You own any object you create.
> You "create" an object using a method whose name begins with “alloc”
> or “new” or contains “copy” (for example, alloc, newObject, or
> mutableCopy).
>
> - If you own an object, you are responsible for relinquishing
> ownership when you have finished with it.
> One way to relinquish ownership of an object is to send it a release
> message. In Cocoa terminology, relinquishing ownership of an object is
> typically referred to as "releasing" an object.
>
> - If you do not own an object, you must not release it.
>
>
>
> > So my doubts are:
> > Since is initialized by a class method , "defaults" must not be
> > released -> correct?
>
> Correct. You didn't create it or retain it, you don't release it
>
> > rawArray should be released? ([rawArray release] ?) I think the answer
> > is no but why?
>
> Correct. You didn't create it or retain it, you don't release it
>
> > dic should be released? ([dic release]?) I don't know...
>
> You didn't create it or retain it, you don't release it
>
> > tempArray should be released? I think the answer is: it depends.
> > Since theArray is retain I could do
> >
> > self.theArray = tempArray;
> > [tempArray release];
> >
> > or
> >
> > theArray = tempArray;
> >
> > so, what is the difference between using self and assigning tempArray
> > to theArray directly?
>
> Assigning directly to the instance variable is just that; an
> assignment. No retain happens, no release happens, no method is
> called. Assigning through the property (i.e. self.theArray =) calls a
> method. That method will release the old value of theArray and retain
> the new value.
>
> > (specifically in this case what is the
> > difference (in terms of retain count)?)
> > And what would happen with the retaincount of another object that was
> > theArray before tempArray assignation? (in case of doing theArray =
> > tempArray)
>
> In this case, not using the property will cause whatever theArray
> *used* to point to to be leaked.
>
> --
> Clark S. Cox III
> clarkc...@gmail.com
>



-- 
_____________________________________________
Guillermo Ignacio Enriquez Gutierrez
_____________________________________________
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to