title: The correct way to declare, alloc, load, and dealloc an NSMutableArray
body: <p>I declare my array in my *.h file:</p>
  
  <pre>
  @interface aViewController: UIViewController
  {
     NSMutableArray *anArray;    // You will need to later change this many times.
  }
  @end
  </pre>
  
  <p>I alloc memory for it my *.m file:</p>
  
  <pre>
  -(void) viewDidLoad
  {
     anArray = [[NSMutableArray alloc] init];
  }
  </pre>
  
  <p>I click a test-button to load my array (eventually it will need to load DIFFERNT values
  on each click):</p>
  
  <pre>
     anArray = [NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil];
  </pre>
  
  <p>And I free it here:</p>
  
  <pre>
  -(void) dealloc
  {
     [anArray release];
     [super dealloc];
  }
  </pre>
  
  <p>Does that all look ok?   </p>
  
  <p>Because it crashes when I later run this code:</p>
  
  <pre>
     NSLog(@"%d", [anArray count]);
  </pre>
  
  <p>Not sure why a simple "NSLog() and count" would crash everything.</p>
  
  <hr>
  
  <p>Dirk,</p>
  
  <p>Let me put it this way:  I have a <em>HUGE</em> misunderstanding of pointers, arrays, strings, and memory.</p>
  
  <p>I've read everything I can find on it... but (yet) to find a simple, clear, easy-to-understand description.</p>
  
  <p>Can you suggest one?  (Hopefully less than 10 pages of reading.)
  Is there a reference that explains <em>JUST</em> this topic... and from a standpoint of "you have 12 years of coding experience... but NONE that ever dealt with allocating memory or pointers".)  </p>
  
  <p>So the variable-name is <em>NOT</em> the way I refer to the object?
  Then why have it?</p>
  
  <p>I'm used to many other languages that just do this:</p>
  
  <pre>
  myString = "this"
  myString = "that"
  
  myInt = 5
  myInt = 15
  </pre>
  
  <p>(What could be simpler.)</p>
  
  <hr>
  
  <p>Looks to me like this would be the easiest way to do this.
  (And it seems to work.  But it is truly correct?)</p>
  
  <pre>
  Don't alloc any memory for my NSMutableArray initially.
  Don't re-alloc any memory repeatedly.  (When I change my array's values.)
  Don't release it repeatedly. (Before I change my array's values.)
  Don't release it when I exit the program.
  
  But:
  Always remember to use RETAIN when I initially assign 
  (and repeatedly reassign) new values to my anArray variable.
  </pre>
  
  <hr>
  
  <blockquote>
    <p>You are not loading your array with 
    anArray = [NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil]; 
    Instead, you are replacing it with a new instance, and worse: with an instance, whose 
    reference is actually owned by some entity you do not control </p>
  </blockquote>
  
  <p>Wow. So I could have 20 arrays... all called the same name:  anArray... and they would be all different?  (There's no such thing as a GLOBAL array?)</p>
  
  <blockquote>
    <p>etc. In order to clear out old values, the method removeAllObject may be handy. 
    There are also mutation methods, which may be used to add multiple values at once.</p>
  </blockquote>
  
  <p>So... first I have to "remove all the objects"... and then I can call <em>ONE</em> method it re-add all my new values.</p>
  
  <blockquote>
    <p>anArray = [[NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil] retain]; 
    instead of the alloc/init sequence.</p>
  </blockquote>
  
  <p>Wow.  I thought nothing could be stored in an array without alloc'ing space for it.</p>
  
  <blockquote>
    <p>If you really intend to replace the entire array, you may want to consider 
    using properties</p>
  </blockquote>
  
  <p>How would I do that using properties?<br>
  What would be the correct way to do something like this:</p>
  
  <pre>
  > anArray = [NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil];
  > anArray = [NSMutableArray arrayWithObjects:@"four", @"five", @"six", nil];
  </pre>
  
  <p>Just like I would do:</p>
  
  <pre>
  x = 12;
  x = 24;
  </pre>
  
  <hr>
  
  <p>Wow.  I <em>REALLY</em> have totally misunderstand everything about strings, arrays, and memory.
  I thought the "easy way" was to alloc ONCE... use the mutable array... change it as much as you want... and free it ONCE.</p>
  
  <blockquote>
    <p>The problem with doing so is that this new array isn't retained, </p>
  </blockquote>
  
  <p>I would think the old array would be gone... and the new array could be used.
  (But I guess not.)</p>
  
  <blockquote>
    <p>Further, you have a memory leak because you never freed the original array.</p>
  </blockquote>
  
  <p>I was thinking the old array wasn't suppose to be freed... I'm not done with it... I just wish to CHANGE it to contain my new values.   (But I guess not.)</p>
  
  <blockquote>
    <p>but one is to use [anArray release]; </p>
  </blockquote>
  
  <p>I was thinking that would cause me to release the memory I allocated... (but I guess not)... and then I'd have to re-alloc more memory.  (But I guess not.)</p>
  
  <blockquote>
    <p>anArray = [[NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil] retain];</p>
  </blockquote>
  
  <p>So I have to "retain" it... so it doesn't disappear out from under me?
  (Not sure why it would.  Until <em>I</em> tell it to... in my final dealloc call.)</p>
  
  <blockquote>
    <p>Another, probably more correct way to fix it would be to use the addObject: or
    addObjectsFromArray: NSMutableArray methods instead of constantly creating new arrays.</p>
  </blockquote>
  
  <p>I only want to create <em>ONE</em> array... and just use it as I want to.
  I never want to <em>ADD</em> to the array.  I want to set it to my new values.</p>