So I am trying to fix this really annoying bug. If I filter my array like the ideal version with NSPredicate, I will get EXC_BAD_ACCESS because it tries to call release on the object passed in as the delegate an extra time. If I filter with the working version, it works fine. I thought these two implementations were identical. Where am I going wrong? I know the predicate way is the way to go, just can't get it to work correctly.
// Ideal version
- (NSArray *)foosWithDelegate:(id)delegate {
return [foos filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"delegate = %@", delegate]];
}
// Working version
- (NSArray *)foosWithDelegate:(id)delegate {
NSMutableArray *results = [[NSMutableArray alloc] init];
for (MYFoo *foo in foos) {
if (foo.delegate == delegate) {
[results addObject:foo];
}
}
if ([results count] == 0) {
[results release];
return nil;
}
return [results autorelease];
}
foos
is an ivar. The MYFoo
class has a property for delegate that is assign
. The issue still happens even if foos
is empty.