With ARC
test1:
@interface test01ViewController ()
@property (strong) void(^myBlock)(id obj, NSUInteger idx, BOOL stop);
@end
@implementation test01ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.navigationItem.title = @"test01";
[self setMyBlock:^(id obj, NSUInteger idx, BOOL stop) {
[self doSomethingWithObj:obj];
}];
}
object (self
) has an explicit strong reference to the block. And the block has an implicit strong
reference to self
. That's a cycle, and now neither object will be deallocated properly.
so test1 dealloc
not calling.
test2:
@interface test03ViewController ()
@property (strong) void(^myBlock)(id obj, NSUInteger idx, BOOL stop);
@end
@implementation test03ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.navigationItem.title = @"test03";
__weak test03ViewController *weakSelf = self;
[self setMyBlock:^(id obj, NSUInteger idx, BOOL stop) {
__strong test03ViewController *strongSelf = weakSelf;
[strongSelf doSomethingWithObj:obj];
}];
}
__weakSelf - > __strongSelf, I think it not difference with test1
, but test2 can calling dealloc
.
Why?