Question

Note: I am posting this as a reference for other developers that might run into the same issue.

Why do I have a memory leak with this code:

@interface SPWKThing : NSObject
@property (strong, nonatomic) NSArray *things;
@end

@implementation SPWKThing {
    BOOL _isKVORegistered;
}

- (id)init
{
    self = [super init];
    if (self) {
        NSLog(@"initing SPWKThing");
        [self registerKVO];
    }
    return self;
}

- (void)didChangeValueForKey:(NSString *)key {
    if ([key isEqualToString:@"things"]) {
        NSLog(@"didChangeValueForKey: things have changed!");
    }
}

#pragma mark - KVO
- (void)registerKVO
{
    if (!_isKVORegistered) {
        NSLog(@"Registering KVO, and things is %@", _things);
        [self addObserver:self forKeyPath:@"things" options:0 context:NULL];
        _isKVORegistered = YES;
    }
}

- (void)unregisterKVO
{
    if (_isKVORegistered) {
        NSLog(@"Unregistering KVO");
        [self removeObserver:self forKeyPath:@"things"];
        _isKVORegistered = NO;
    }
}

- (void)dealloc
{
    NSLog(@"SPWKThing dealloc");
    [self unregisterKVO];
}

@end

@implementation SPWKViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self runDemo];
}

- (void)runDemo
{
    SPWKThing *thing = [[SPWKThing alloc] init];
    thing.things = @[@"one", @"two", @"three"];
    thing = nil;
}

@end

My output is:

initing SPWKThing
Registering KVO, and things is (null)
didChangeValueForKey: things have changed!

dealloc is never called? Why? I am setting thing = nil in the last line of runDemo!

See a demo project here: https://github.com/jfahrenkrug/KVOMemoryLeak

Was it helpful?

Solution

The answer is:

Never override didChangeValueForKey: (at least not without calling super). The documentation does not warn you about this.

Use the correct method observeValueForKeyPath:ofObject:change:context: instead.

This project clearly demonstrates this: https://github.com/jfahrenkrug/KVOMemoryLeak

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