NSPredicate Memory Problems

So, I'm trying to fix this annoying mistake. If I filter my array as an ideal version with NSPredicate, I will get EXC_BAD_ACCESS because it is trying to trigger a release on the object passed as a delegate, 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 that the predicate method is the way, it just cannot make it 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];
}

foosis ivar. The class MYFoohas a property for the delegate, which assign. The problem still occurs, even if foosempty.

+3
source share
1 answer

dealloc PredicateTestViewController foos, .

// Your code in PredicateTestViewController.m
- (void)dealloc
{
    [foos dealloc];
    [super dealloc];
}

// Your new code in PredicateTestViewController.m
- (void)dealloc
{
    [foos release];
    [super dealloc];
}
+1

Source: https://habr.com/ru/post/1722143/


All Articles