一个对象中强引用了block,在block中又强引用了该对象,就会发生循环引用。
1、变量在block中使用
__block int a = 0;
NSLog(@"1###%p", &a); //栈区
void (^foo)(void) = ^{
a = 1;
NSLog(@"2###%p", &a); //堆区
};
foo();
NSLog(@"3###%p", &a); //堆区
2017-08-08 10:59:24.661 animateProject[12371:155504] 1###0x7fff5cfa0a18
2017-08-08 10:59:24.661 animateProject[12371:155504] 2###0x600000033098
2017-08-08 10:59:24.661 animateProject[12371:155504] 3###0x600000033098
2、对象在Block使用
#define WS(weakS,obj) __weak __typeof(&*obj)weakS = obj;
id __weak weakSelf = self;
id __block weakSelf = self;
__weak __typeof(&*self)weakSelf = self