Skip to content

Commit

Permalink
Actually fix deallocation behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Tricertops committed Dec 16, 2016
1 parent 135a8c6 commit 7591d02
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Sources/MTKDeallocator.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ @implementation MTKDeallocator
- (instancetype)initWithOwner:(NSObject*)owner {
self = [super init];
if (self) {
self->_owner = nil;
self->_owner = owner;
self->_callbacks = [NSMutableArray new];
}
return self;
Expand All @@ -40,9 +40,13 @@ - (void)addCallback:(MTKDeallocatorCallback)block {


- (void)invokeCallbacks {
NSArray<MTKDeallocatorCallback> *blocks = self->_callbacks;
self->_callbacks = nil;

__unsafe_unretained NSObject *owner = self->_owner;
for (MTKDeallocatorCallback block in self->_callbacks)
for (MTKDeallocatorCallback block in blocks) {
block(owner);
}
}


Expand Down
4 changes: 4 additions & 0 deletions Tests/BlockObservingTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ - (void)testRemoveAllObservationsForKeyPath {
- (void)testAutomaticRemoveOnDealloc {
MTKTestingObject *object = [MTKTestingObject new];
[self observeObject:object property:@keypath(object, title) withBlock:^(id self, id object, id old, id new) {}];
__weak NSSet *observations = [object valueForKey:@"mtk_keyPathBlockObservers"][@"title"];

XCTAssertEqual(observations.count, 1);
XCTAssertNoThrow({ object = nil; });
XCTAssertEqual(observations.count, 0);
}

@end

0 comments on commit 7591d02

Please sign in to comment.