views:

114

answers:

1

If I try and release tempSeedsArray after seedsArray = tempSeedsArray , I get an EXEC_BAD_ACCESS, and Instruments shows that tempSeedsArray has been released twice. Here is my viewWillAppear method:

- (void)viewWillAppear:(BOOL)animated {  
    NSString *arrayFilePath = [[NSBundle mainBundle] pathForResource:@"SeedsArray" ofType:@"plist"];  
    NSLog(@"HIT!");  
    NSMutableArray *tempSeedsArray = [[NSMutableArray alloc] initWithContentsOfFile:arrayFilePath];  
    seedsArray = tempSeedsArray;  
    NSLog(@"%u", [seedsArray retainCount]);  
    [seedsArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];  
    [super viewWillAppear:animated];  
}

seedsArray is an NSMutableArray set as a nonatomic and a retain property, and is synthesised.

However, if I change seedsArray = tempSeedsArray to self.seedsArray = tempSeedsArray (or [self seedsArray] = tempSeedsArray etc.), I can release tempSeedsArray. Could someone please explain simply to me why this is, as I am very confused!

Thanks

+2  A: 
  1. seedsArray = ... assigns to the seedsArray field.
  2. self.seedsArray = ... invokes setSeedsArray:, which is the setter for the seedsArray property.

If you @synthesize seedsArray, these two form will behave almost the same (see @JeremyP's comments below), but if you define your own setter, only the second form will set the property and invoke your code.

Marcelo Cantos
so then why is it that although I have synthesized `seedsArray`, `seedsArray = ...` and `self.seedsArray = ...` seem to behave differently? (I do not have a custom setter)
jrtc27
Because those two lines are not the same if you synthesize seedsArray. The first one merely assigns the tempSeedsArray pointer to the seedsArray instance variable. The second sends a setSeedsArray: message to the object with the property which does three things: 1. retains the object referenced by tempSeedsArray, 2. assigns tempSeedsArray to the instance variable for the property, 3. releases the object previously referenced by the instance variable.
JeremyP
ok thanks people ;)
jrtc27
+1 @JeremyP, I didn't consider the `retain` property qualifier.
Marcelo Cantos