• 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: Erik Buck <email@hidden>
  • Date: Fri, 20 Jan 2006 11:39:10 -0500

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.


One correct answer is the following:
- (void)setMyString:(NSString *)newString
{
[myString autorelease]; // after this method returns, forget about the old value of myString
myString = [newString copy];
}



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

Accessorizer will even automate the creation of this code so that you don't need to type it.
http://www.kevincallahan.org/software/accessorizer.html


Folks, please! I implore you. You can't write good Cocoa code in Objective-C unless and until you master the very simple and if I may say so elegant memory management rules. It is one thing to ask a question when learning. It is another thing entirely to give a wrong answer.


The bottom line is that reference counting is a tried and true technique
that has been in use practically since dynamic memory allocation was
invented. It works well with distributed systems (probably one reason it
was used with Openstep) when garbage collection does not.


I should add the Microsoft's COM/DCOM use reference counting. MFC CString is reference counted.

If you didn't allocate it, copy it, or retain it, don't release it or
autorelease it. If you did allocate, copy, or retain an object, release it
unless you want to return the object from a method or function in which case
autorelease it so it will still be valid long enough for someone else to
retain it.


_______________________________________________
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


  • Follow-Ups:
    • Re: Some confusion on memory management
      • From: Scott Ellsworth <email@hidden>
    • Re: Some confusion on memory management
      • From: Jeff Gilbert <email@hidden>
  • Prev by Date: Using NSAppleEventDescriptor with NSDates
  • Next by Date: Re: Some confusion on memory management
  • Previous by thread: Re: Some confusion on memory management
  • Next by thread: Re: Some confusion on memory management
  • Index(es):
    • Date
    • Thread