Fix KVO-related crashes in SVGKFastImageView when setting the .image property

I'm creating an SVGKFastImageView and setting its .image property instead of passing a SVGKImage to its initializer. When the SVGKImageView is later deallocated I get KVO-related crashes, saying it couldn't remove an observer that was never added. This patch fixes the issue for me on iOS 8, although based on the code I suspect the same problems would happen on earlier versions of iOS. The fix is to register all the observers in setImage instead of in populateFromImage, so that they're always registered when dealloc tries to unregister them. The dealloc method also changes to not call setImage (which would re-register them again), and instead releases the _image field directly.

The other change that is required for me to not crash on iOS 8 is to un-comment removing the "DOMTree.viewport" observer in SVGKImage, but that's not part of this pull request because I saw there's a separate issue for it.
Not a stylistic choice at all. Setting self.image to nil would call [self setImage:nil] thanks to property rewriting, and in doing so would re-register all the observers.

On Oct 20, 2014, at 11:51 AM, MaddTheSane <notifications@github.com> wrote:

> In Source/UIKit additions/SVGKFastImageView.m:
> 
> > @@ -184,7 +177,12 @@ - (void)dealloc
> >  	[self removeObserver:self forKeyPath:@"tileRatio" context:internalContextPointerBecauseApplesDemandsIt];
> >  	[self removeObserver:self forKeyPath:@"showBorder" context:internalContextPointerBecauseApplesDemandsIt];
> >      
> > -	self.image = nil;
> > +    if (_image) {
> > +        [_image removeObserver:self forKeyPath:@"size" context:internalContextPointerBecauseApplesDemandsIt];
> > +    }
> > +    
> > +    [_image release];
> You might want to add self.image = nil; back in, although doing so would purely be a stylistic choice.
> 
> —
> Reply to this email directly or view it on GitHub.
>