Skip to content
This repository has been archived by the owner on Jun 2, 2018. It is now read-only.

NSObject(BlockObservation) and automatic removal upon deallocation #115

Closed
SaturnPolly opened this issue Jan 3, 2013 · 1 comment
Closed
Assignees
Labels

Comments

@SaturnPolly
Copy link

The docs for NSObject(BlockObservation) do not explicitely say that BlockObservers will be removed autamatically upon dealloction, but it is more or less implied in the following warning (at least for iOS 5 and newer):

Warning: Important: Due to a design flaw in some recent versions of the Objective-C runtime, you must call either a block overserver removal method in the dealloc method of any object making use of block-backed key-value observing. This has been fixed in the OS X 10.7 and iOS 5.0 runtime.

When running the following example on iOS6 however the observer is not actually removed:

#import <Foundation/Foundation.h>
#import "BlocksKit.h"

int main(int argc, char *argv[]) {
    @autoreleasepool {
        NSObject * foo = [NSObject new];
        [foo addObserverForKeyPath:@"bar" task:^(id sender) {}];
        return 0;
    }
}

This will generate the following log entry:

An instance 0x9b47210 of class NSObject was deallocated while key value 
observers were still registered with it. Observation info was leaked, and 
may even become mistakenly attached to some other object. Set a breakpoint 
on NSKVODeallocateBreak to stop here in the debugger. 
Here's the current observation info:
<NSKeyValueObservationInfo 0x9b49b30> (
<NSKeyValueObservance 0x9b49950: 
    Observer: 0x9b47d90, 
    Key path: bar, 
    Options: <New: NO, Old: NO, Prior: NO> 
    Context: 0xc3fc4, 
    Property: 0xb01f270>
)

To clarify: the observee is deallocated correcty, the BKObserver instance is deallocated correctly, but the observation info remains in memory.

So, did I misunderstand the features of this category or is this a bug?

@zwaldowski
Copy link
Collaborator

In theory the OS should be taking care of it but this is potentially a bug; it'll be looked into.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants