Search code examples
iosmemory-managementdelegatesnsoperationdealloc

delegate deallocated during the operation


I'm looking for a solution that solves the following problem:

I have a NSOperation which download the image in the background:

@protocol CoreImageDownloadingOperationDelegate <NSObject>

@required
-(void) handleResponse:(UIImage*) response;
-(void) handleException:(MobileServiceException*) exception;

@end

@interface CoreImageDownloadingOperation : NSOperation{
}

-(id) initWithDelegate:(id<CoreImageDownloadingOperationDelegate>)del andImageID: (NSString *) image;

@property (nonatomic, assign) id <CoreImageDownloadingOperationDelegate> delegate;

When it's finish the downloading, calling the delegate method, to set the image to the imageView:

pragma mark - overridden from NSOperation
- (void) main {

    if (self.isCancelled)
        return;

    @autoreleasepool {

        @try {

            UIImage* image = [[CoreEnvironment sharedInstance] getImageFromServer: [imageID stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

            if (self.isCancelled)
                return;

            if(delegate){
                [delegate handleResponse:image];
            }
            else
                NSLog(@"CachedImageView already deallocated");

        }@catch (NSException *exception) {

            TestLog(@"%@", exception.reason);

            if (self.isCancelled)
                return;

            if(delegate && [exception isKindOfClass:[MobileServiceException class]])
                [delegate handleException:(MobileServiceException*)exception];

        }
    }
}

The problem is: when I go to another page while the image is downloading, the cachedImageView is deallocated, but when the imageDownloadingOperation finishes downloading, the delegate is not nil, and it's trying to handleResponse... And of course I get message sent to deallocated...

I alloc init the operation like this in the CachedImageView:

CoreImageDownloadingOperation* imageDownloadingOperation = [[CoreImageDownloadingOperation alloc] initWithDelegate:self andImageID:imageKey];

enter image description here or:

enter image description here

-[CachedImageView isKindOfClass:]: message sent to deallocated instance 0x18868550

Solution

  • Where is your protocol declaration? I'd expect to see this:

    @protocol CoreImageDownloadingOperationDelegate <NSObject> 
    
    - (void) handleResponse:(UIImage *) image;
    
    @end
    
    @interface CoreImageDownloadingOperation : NSOperation{
    }
    
    -(id) initWithDelegate:(id<CoreImageDownloadingOperationDelegate>)del andImageID: (NSString *) image;
    
    @property (nonatomic, assign) id <CoreImageDownloadingOperationDelegate> delegate;
    

    You are getting the warning/crash because it can't find the responder handleResponse:

    Also when invoking the delegate your better off doing:

    if ([self.delegate respondsToSelector:@selector(handleResponse:)])
        [self.delegate handleResponse:image];
    

    You don't need to check if (self.delegate && [self.delegate responds .... as it will return nil if the delegate is nil && if the selector is not implemented.

    EDIT *

    Where you create:

    CoreImageDownloadingOperation* imageDownloadingOperation = [[CoreImageDownloadingOperation alloc] initWithDelegate:self andImageID:imageKey];
    

    I suspect this is being released, turn this into a property of the class it's in. Then try again (make sure to release it when you're done though) i.e

    In your .h

    @property (nonatomic, retain) CoreImageDownloadingOperation* imageDownloadingOperation;
    

    Then initialise with:

    if (!self.imageDownloadingOperation)
        self.imageDownloadingOperation = [[CoreImageDownloadingOperation alloc] initWithDelegate:self andImageID:imageKey];