title: release/autorelease confusion in cocoa for iphone
body: <p>I'm slowly teaching myself cocoa for the iPhone(through the <a href="http://www.stanford.edu/class/cs193p/cgi-bin/index.php" rel="nofollow noreferrer">Stanford Class on iTunes U</a>) and I've just gone through the part on memory management, and I wanted to hopefully get some confirmation that the assumptions I'm making on how memory is handled and how [release] and [autorelease] work.  Since memory management is a really basic and fundamental, but very essential part of the programming experience, I'd like to make sure I'm doing it right.</p>
  
  <p>I understand that anything with an alloc, new, or copy needs to be released.<br>
  If I do this:</p>
  
  <pre><code>NSString *temp = [[NSString alloc] initWithString:@"Hello World"];
  </code></pre>
  
  <p>Then I need to add [temp release/autorelease] eventually, since I have an alloc.</p>
  
  <p>However, if I do this: </p>
  
  <pre><code>NSString *temp = @"Hello World";
  </code></pre>
  
  <p>Then it doesn't seem to need a release statement.  Does the NSString class call autorelease automatically as part of the assignment?</p>
  
  <p>Also, is there any difference between the two *temp objects here after these statements?  They both contain the same string, but are there memory/usage ways where they differ?  </p>
  
  <p>Secondly, with properties, I'm assuming that the autorelease is handled automatically.  If I have this:</p>
  
  <pre><code>@interface Person : NSObject
  {
      //ivars
      NSString *firstName;
      NSString *lastName;
  }
  
  //properties
  @property NSString *firstName;
  @property NSString *lastName;
  
  ///next file
  
  @implementation Person
  
  @synthesize firstName;
  @synthesize lastName;
  
  - (void) dealloc
  {
  
      //HERE!!!!
  
      [super dealloc];
  }
  </code></pre>
  
  <p>I'm assuming I don't need to add [firstName release] and [lastName release] (at //HERE!!!!), since that's automatically handled by the properties.  Is that correct?</p>
  
  <p>I do understand that if I do this in code(assuming I've defined initWithFirstName):</p>
  
  <pre><code>Person *Me = [[Person alloc] initWithFirstName: @"Drew", lastName:"McGhie"];
  </code></pre>
  
  <p>that later I'm going to have to use [Me release/autorelease];</p>
  
  <p>Any help confirming or correcting my understanding so far is greatly appreciated.  </p>
  
  <blockquote>
    <p><strong>POST ANSWER WRITE-UP</strong></p>
  </blockquote>
  
  <p>I thought I'd write this all up after going over all the answers and testing out the suggestions and talk about what worked.  </p>
  
  <p>I do need to add the [firstName release], [lastName release], but I also need to add (retain) to the property descriptions.  Not adding the (retain) caused warnings because it assumes (assign).  Here's how I finally set up the class</p>
  
  <pre><code>@interface Person : NSObject
      {
          //ivars
          NSString *firstName;
          NSString *lastName;
      }
  
      //properties
      @property (retain) NSString *firstName;
      @property (retain) NSString *lastName;
  
      ///next file
  
      @implementation Person
  
      @synthesize firstName;
      @synthesize lastName;
  
      - (void) dealloc
      {
          [firstName release];
          [lastName release];
          [super dealloc];
      }
  </code></pre>