• 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: Some confusion on memory management
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Some confusion on memory management


  • Subject: Re: Some confusion on memory management
  • From: Jeff Gilbert <email@hidden>
  • Date: Fri, 20 Jan 2006 11:17:52 -0600

Everyone,

On Friday, January 20, 2006, at 10:39AM, Erik Buck <email@hidden> wrote:

>Arggggg!
>
>I have resisted these threads for a long time, but I just can't let
>incorrect answers persist...
>
>// The following code is WRONG!
>- (void)setMyString:(NSString *)newString
>{
>    [myString release];  // forget about the old value of myString
>    myString = [newString copy];
>}
>
>
>In the above code, if newString and myString point to the same
>object, newString will already be released and possibly deallocated
>before -copy is sent to it.

Of course Erik is right. Unfortunately, I left out one critical piece from the way I usually write setters:


- (void)setMyString:(NSString *)newString
{
    if (myString != newString)
    {
        [myString release];  // forget about the old value of myString
        myString = [newString copy];
    }
}

(I really shouldn't type code directly into mail...)

I apologize for providing misleading information.


>Please consult the many nice explanations:
>"Very simple rules for memory management in Cocoa" http://
>www.stepwise.com/Articles/Technical/2001-03-11.01.html
>
>"Accessor methods revisited" http://www.stepwise.com/Articles/
>Technical/2002-06-11.01.html/
>
>http://www.cocoadev.com/index.pl?MemoryManagement
>
>http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/
>index.html

These are all excellent references and worth reading.

Sorry,
Jeff
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Re: Some confusion on memory management (From: Erik Buck <email@hidden>)

  • Prev by Date: Re: Some confusion on memory management
  • Next by Date: Re: Weird problem with Core Data
  • Previous by thread: Re: Some confusion on memory management
  • Next by thread: Re: Some confusion on memory management
  • Index(es):
    • Date
    • Thread