Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

iphone - 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];

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?

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.

For example: With Properties:

//Header
@property (nonatomic, copy) void (^cancelledBlock)(void);

//Implementation
@sythesize cancelledBlock;

leads to EXC_BAD_ACCESS when setting cancelledBlock

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);

}

there is no EXC_BAD_ACCESS and everything runs as it should.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Basically I had to remove -weak_library /usr/lib/libSystem.B.dylib from the linker flags and replace it with -weak-lSystem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...