objective c - NSMutableArray contained objects and memory management -


i studying memory management, , have made small program manages table object.

here .h:

@interface person : nsobject {         nsstring *firstname;         nsstring *lastname; }  @property (readwrite, copy) nsstring *firstname;  @property (readwrite, copy) nsstring *lastname; 

here .m:

- (ibaction)clic2:(id)sender {     person *pers = [[person alloc]init];     nsmutablearray *myarray = [[nsmutablearray alloc] init];      [pers setfirstname:@"fn 1"]; // = pers.firstname     [pers setlastname:@"ln 1"]; // = pers.lastname     [myarray addobject:pers];      [pers setfirstname:@"fn 2"];     [pers setlastname:@"ln 2"];     [myarray addobject:pers];      [pers release];       for(int = 0; < [myarray count]; i++)         {         pers = [myarray objectatindex:i];         nslog(@"%d %@ %@ %@", [myarray objectatindex:i], pers, pers.firstname, pers.lastname);         }  }   - (void)dealloc {     [firstname release];     firstname = nil;      [lastname release];     lastname = nil;      [super dealloc]; } 

and nslog

 2011-04-28 21:40:11.568 temp[4456:903] 4495296 <person: 0x1004497c0> fn 2 ln 2 2011-04-28 21:40:11.571 temp[4456:903] 4495296 <person: 0x1004497c0> fn 2 ln 2 

as can see, stored information same; seems because myarray stores same memory address/content of person in array.

i understand when change content of pers @ second call system change information in myarray.

how do not overwrite data?

thank answers.


edit : example 2 persons idea manage unbounded number given


edit2 : thank's memory management explanations in case

the array doesn't copy objects added it, retains them. you've got 1 person object scope of -clic2: method. set data , add array. array stores pointer person object , increments retain count. still have 1 person object, change first or last name overwrite original data set. you've got in array @ end of -clic2: method 2 references same person object.

what want this:

- ( ibaction )buttonclicked: (id)sender {     person            *person;     nsmutablearray    *array;     nsinteger         i, count = 10;      array = [[ nsmutablearray alloc ] initwithcapacity: count ];     ( = 1; <= count; i++ ) {         /* create new person object on each iteration through loop */         person = [[ person alloc ] init ];         /* person has -retaincount of @ least +1 after allocation */          person.firstname = [ nsstring stringwithformat: @"fn %d", ];         person.lastname = [ nsstring stringwithformat: @"ln %d", ];          /* adding object increments person retaincount @ least +2 */         [ array addobject: person ];          /*          * -release decrements retaincount, when release array          * @ end of method , sends release objects          * memory allocated person objects reclaimed.          */         [ person release ];     }      ( person in array ) {         nslog( @"%@ %@ %@", person, person.firstname, person.lastname );     }      /* releasing array sends -release objects */     [ array release ]; } 

Comments