复制或保留块时为EXC_BAD_ACCESS [英] EXC_BAD_ACCESS when copying or retaining Block
问题描述
据我所知,块的作用类似于对象,因为您可以向其发送复制或释放消息,例如:
As far as I understand a Block acts like an object, in that you can send copy or release messages to it, e.g:
[myBlock copy];
但是无论何时执行此操作或释放一个块,我都会得到EXC_BAD_ACCESS.
However whenever I do this, or release a block, I get EXC_BAD_ACCESS.
如果我使用块函数,那么一切都会按预期进行,例如:
If I use the block functions, everything works as expected, e.g.:
Block_copy(myBlock);
我认为释放和复制块的方式是相同的吗?
I thought both ways of releasing and copying blocks were identical?
问题不大,但是如果我有一个属性(副本)是一个块,那我就必须自己编写setter方法.
It's not that much of a problem, but it is a little annoying that if I have a property (copy) which is a Block, I have to write the setter method myself.
例如:具有属性:
//Header
@property (nonatomic, copy) void (^cancelledBlock)(void);
//Implementation
@sythesize cancelledBlock;
设置cancelledBlock
导致EXC_BAD_ACCESS
但如果我这样做:
but if I do:
//Header
@property (nonatomic, copy) void (^cancelledBlock)(void);
//Implementation
@sythesize cancelledBlock; //saves me doing the getter as well
- (void)setCancelledBlock:(void (^)(void))aCancelledBlock {
if (cancelledBlock == aCancelledBlock) {
return;
}
void (^oldValue)(void) = cancelledBlock;
cancelledBlock = Block_copy(aCancelledBlock);
Block_release(oldValue);
}
没有EXC_BAD_ACCESS,一切运行正常.
there is no EXC_BAD_ACCESS and everything runs as it should.
推荐答案
After a long and boring afternoon and evening I finally came across this answer here, although it may seem unrelated, the chain of websites I visited to find it, creates that relation.
基本上,我必须从链接器标志中删除-weak_library /usr/lib/libSystem.B.dylib
并将其替换为-weak-lSystem
.
Basically I had to remove -weak_library /usr/lib/libSystem.B.dylib
from the linker flags and replace it with -weak-lSystem
.
这篇关于复制或保留块时为EXC_BAD_ACCESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!