• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: should release or not? (basic memory management question)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: should release or not? (basic memory management question)


  • Subject: Re: should release or not? (basic memory management question)
  • From: Ignacio Enriquez <email@hidden>
  • Date: Mon, 30 Mar 2009 04:53:33 +0900

Thanks Clark. Cleared my doubts ;)


On Mon, Mar 30, 2009 at 4:38 AM, Clark Cox <email@hidden> wrote:

> On Sun, Mar 29, 2009 at 12:24 PM, Ignacio Enriquez <email@hidden>
> 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
> email@hidden
>



--
_____________________________________________
Guillermo Ignacio Enriquez Gutierrez
_____________________________________________
_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:

This email sent to email@hidden

References: 
 >should release or not? (basic memory management question) (From: Ignacio Enriquez <email@hidden>)
 >Re: should release or not? (basic memory management question) (From: Clark Cox <email@hidden>)

  • Prev by Date: Re: Spotlight and Leopard
  • Next by Date: [BUG?] - [NSLayoutManager usedRectForTextContainer:] returns incorrect value
  • Previous by thread: Re: should release or not? (basic memory management question)
  • Next by thread: [BUG?] - [NSLayoutManager usedRectForTextContainer:] returns incorrect value
  • Index(es):
    • Date
    • Thread