在块中使用弱对象时崩溃
Crash when use weak obj in block
我的代码是这样的:
Member *member = [Member new];
__weak __typeof(self) weakSelf = self
member.gotoPageBlock = ^(NSString *url) {
__strong __typeof(weakSelf) self = weakSelf
[self goToPageWithURL:[NSURL URLWithString:url]];
};
很少会崩溃,这次崩溃的栈顶是这样的:
Exception Type: SIGSEGV
Exception Codes: SEGV_ACCERR at 0x365722298
Triggered by Thread: 0
Thread 0 Crashed:
0 libobjc.A.dylib 0x0000000182794bb4 _objc_loadWeakRetained :156 (in libobjc.A.dylib)
执行块时发生崩溃。
有人知道怎么回事吗?
不需要再次强大自己所以,删除此行__strong __typeof(weakSelf) self = weakSelf
然后应该可以正常工作
使用这个
Member *member = [Member new];
__weak __typeof(self) weakSelf = self
member.gotoPageBlock = ^(NSString *url) {
//the difference is here
[weakSelf goToPageWithURL:[NSURL URLWithString:url]];
};
在执行时失去所有权部分查看此https://sectionfive.net/blog/2014/11/24/arc-exploration-and-pitfalls/以获取更多信息
我的代码是这样的:
Member *member = [Member new];
__weak __typeof(self) weakSelf = self
member.gotoPageBlock = ^(NSString *url) {
__strong __typeof(weakSelf) self = weakSelf
[self goToPageWithURL:[NSURL URLWithString:url]];
};
很少会崩溃,这次崩溃的栈顶是这样的:
Exception Type: SIGSEGV
Exception Codes: SEGV_ACCERR at 0x365722298
Triggered by Thread: 0
Thread 0 Crashed:
0 libobjc.A.dylib 0x0000000182794bb4 _objc_loadWeakRetained :156 (in libobjc.A.dylib)
执行块时发生崩溃。
有人知道怎么回事吗?
不需要再次强大自己所以,删除此行__strong __typeof(weakSelf) self = weakSelf
然后应该可以正常工作
使用这个
Member *member = [Member new];
__weak __typeof(self) weakSelf = self
member.gotoPageBlock = ^(NSString *url) {
//the difference is here
[weakSelf goToPageWithURL:[NSURL URLWithString:url]];
};
在执行时失去所有权部分查看此https://sectionfive.net/blog/2014/11/24/arc-exploration-and-pitfalls/以获取更多信息