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.
In your PredicateTestViewController's dealloc method, you should be releasing foos, not deallocating them.
// Your code in PredicateTestViewController.m
- (void)dealloc
{
[foos dealloc];
[super dealloc];
}
// Your new code in PredicateTestViewController.m
- (void)dealloc
{
[foos release];
[super dealloc];
}
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