Question

I have the following code (in a non-ARC project):

- (void)loadWithCompleteBlock:(void (^)(void))complete
{    
    ...
    complete = [complete copy];
    ...            
    [[NSOperationQueue mainQueue] addObserver:self forKeyPath:@"operationCount" options:0 context:complete];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *) context
{
    void (^complete)(void) = context;
    [self performSelectorInBackground:@selector(loadFilesWithCompleteBlock:) withObject:complete];
    [complete release];
}

The static analyzer gives the warning Potential leak of an object stored into 'complete'

I tired to add NS_RELEASES_ARGUMENT or CF_RELEASES_ARGUMENT to the context parameter, but nothing works.

Any ideas?

Was it helpful?

Solution

Passing an object through a void* and release it in a callback method is something the analyzer cannot understand. You could just silence the analyzer for these cases.

But in this case the code is broken anyway and should be refactored. You cannot use KVO's context to pass an object to the callback: You need the context to identify the observation. See for example Dave Dribin's description of how to do KVO properly.

Can't you just set the block as a completion block to the operation you add?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top