Skip to content

Instantly share code, notes, and snippets.

@bgerstle
Last active August 29, 2015 14:23
Show Gist options
  • Save bgerstle/65b375f6c7780c924ec8 to your computer and use it in GitHub Desktop.
Save bgerstle/65b375f6c7780c924ec8 to your computer and use it in GitHub Desktop.
Retain cycle unit tests
@interface WMFRetainCycleTests : XCTestCase
@property (strong, nonatomic) void(^block)(id);
@end
@implementation WMFRetainCycleTests
- (void)testUsingArgument {
WMFRetainCycleTests* noCycle = [WMFRetainCycleTests new];
__weak WMFRetainCycleTests* weakRef = noCycle;
noCycle.block = ^ (id y) {
NSLog([y description]);
};
noCycle.block(noCycle); //< call block with itself
noCycle = nil; //< nil out strong reference
XCTAssertNil(weakRef); //< weak reference is nil! ✅
}
- (void)testReferencingCycle {
WMFRetainCycleTests* cycle = [WMFRetainCycleTests new];
__weak WMFRetainCycleTests* weakRef = cycle;
cycle.block = ^ (id y) {
NSLog([cycle description]); //< warning: captuing 'cycle' strongly in this block may create a retain cycle
};
cycle.block(cycle); //< try calling with itself for parity w/ previous test
cycle = nil;
XCTAssertNil(weakRef); //< failure! 🚨
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment