I am studying memory management, and I have made a small program which manages a table object.
Here is my .h:
@interface Person : NSObject {
NSString *firstName;
NSString *lastName;
}
@property (readwrite, copy) NSString *firstName;
@property (readwrite, copy) NSString *lastName;
Here is my .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 i = 0; i < [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 this is my 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 you can see, the stored information is the same; it seems that is because myarray always stores the same memory address/content of the person in the array.
I understand from this that when I change the content of pers at the second call the system will also change the information in myarray.
How do I do this so as to not overwrite the data?
Thank you for your answers.
EDIT : This example is for 2 persons but the idea is manage an unbounded number given
EDIT2 : Thank's all for memory management explanations in this case
The array doesn't copy objects added to it, it just retains them. You've got only one Person object for the scope of the -clic2: method. You set the data and add it to the array. The array stores a pointer to the Person object and increments its retain count. But you still only have one Person object, so any change to the first or last name will overwrite the original data you set. What you've got in the array at the end of your -clic2: method is two references to the same Person object.
What you want is something like this:
- ( IBAction )buttonClicked: (id)sender
{
Person *person;
NSMutableArray *array;
NSInteger i, count = 10;
array = [[ NSMutableArray alloc ] initWithCapacity: count ];
for ( i = 1; i <= count; i++ ) {
/* create a new Person object on each iteration through the loop */
person = [[ Person alloc ] init ];
/* person has a -retainCount of at least +1 after allocation */
person.firstName = [ NSString stringWithFormat: @"FN %d", i ];
person.lastName = [ NSString stringWithFormat: @"LN %d", i ];
/* adding the object increments the person retainCount to at least +2 */
[ array addObject: person ];
/*
* -release decrements the retainCount, so when we release the array
* at the end of the method and it sends release to all its objects
* the memory allocated for the Person objects will be reclaimed.
*/
[ person release ];
}
for ( person in array ) {
NSLog( @"%@ %@ %@", person, person.firstName, person.lastName );
}
/* releasing the array also sends -release to all its objects */
[ array release ];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With