Skip to content

Instantly share code, notes, and snippets.

@bjhomer
Created November 1, 2012 23:06
Show Gist options
  • Save bjhomer/3997359 to your computer and use it in GitHub Desktop.
Save bjhomer/3997359 to your computer and use it in GitHub Desktop.
Capturing 'self' leads to a retain cycle inside methods starting with -add
- (void)testAddRemove
{
if (arc4random() % 2) {
NSLog(@"A");
[_other addWithDispatchBlock: ^{
NSLog(@"self: %@", self); // <-- warning about capturing 'self'
}];
}
else {
NSLog(@"B");
[_other removeWithDispatchBlock: ^{
NSLog(@"self: %@", self); // <-- NO warning about capturing 'self'
}];
}
}
- (void)testAddAdd {
if (arc4random() % 2) {
NSLog(@"A");
[_other addWithDispatchBlock: ^{
NSLog(@"self: %@", self); // <-- warning about capturing 'self'
}];
}
else {
NSLog(@"B");
[_other addWithDispatchBlock: ^{
NSLog(@"self: %@", self); // <-- warning about capturing 'self'
}];
}
}
- (void)testRemoveRemove {
if (arc4random() % 2) {
NSLog(@"A");
[_other removeWithDispatchBlock: ^{
NSLog(@"self: %@", self); // <-- NO warning about capturing 'self'
}];
}
else {
NSLog(@"B");
[_other removeWithDispatchBlock: ^{
NSLog(@"self: %@", self); // <-- NO warning about capturing 'self'
}];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment