iOS: Block property directly set crashes when accessed

前提是你 提交于 2019-12-05 02:39:25
- (void)giveBlock:(void(^)())inBlock {
    blockCopy = inBlock;
}

You need to copy the block either on assignment or when passed into this function. While ARC solves the auto-move-to-heap-on-return problem, it does not do so for arguments (can't do to idiosyncrasies of C).

That it doesn't crash in certain environments is merely coincidental; it won't crash as long as the stack version of the block hasn't been overwritten. A sure sign of this is when you have a crash that goes away with optimization turned off. With optimization off, the compiler won't reuse stack memory within any given scope, causing the memory to be "valid" for long after it should be.


I still don't quite understand why it can't do a objc_blockRetain rather than a normal objc_retain, though. The compiler knows the type after all.

I'm pretty sure that the issue is the potential cost of the assignment. If the block captures a lot of state, including, potentially, other blocks, then the Block_copy() might be really really really expensive.

I.e. if you had something like:

BlockType b = ^(...) { ... capture lots of gunk ... };
SomeRandomFunc(b);

... and that implied a Block_copy() merely because of the assignment, it would make it impossible to use blocks consistently without risk of pathological performance issues. Because there is no way for the compiler to know whether SomeRandomFunc() is synchronous or asynchronous, there is no way to manage this automatically (at this time -- I'm sure getting rid of this potential tripwire is desirable).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!