16

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.

Jonathan.
  • 53,997
  • 54
  • 186
  • 290
  • You should only release what you retained or copied previously. – Richard Aug 18 '11 at 18:10
  • I know that, as I say in my question trying to do `[myBlock copy]` gives EXC_BAD_ACCESS, but `Block_copy(myBlock)` doesn't – Jonathan. Aug 18 '11 at 18:12
  • Can you show us a full example? – Joshua Weinberg Aug 18 '11 at 18:15
  • There aren't really examples, everywhere I use blocks it is the same. – Jonathan. Aug 18 '11 at 18:17
  • 1
    Copied block properties work just fine. If they don't in your case, you should create and upload a simple demonstration project that we can try. – zoul Aug 18 '11 at 18:24
  • I just created a demo project, and it works fine in that. the project I'm working in now which has this problem was created in Xcode 3.2, before Xcode 4 and iOS5 (with ARC, maybe that makes a difference, I turned it off though in the demo project). – Jonathan. Aug 18 '11 at 18:37
  • What compiler and IDE are you running into the issue with? If its GCC flip to Clang – Joshua Weinberg Aug 18 '11 at 20:05

1 Answers1

26

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.

Community
  • 1
  • 1
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
  • Thank you, Jonathan! After after more than 4 hours you solved my problem! – Heiko Behrens Sep 14 '11 at 14:49
  • Whoa. I have literally spent weeks trying to track down what was causing my app to crash when launching in the simulator. This answer did it. Thank you so much! – Kyle Slattery Sep 16 '11 at 05:17
  • I know how annoying this problem is, I just wish I knew why this solution works. – Jonathan. Sep 16 '11 at 07:12
  • I wish there is a donate system in SO so I could give you 1000 points for this answer. I am not sure whether I could have found the cause by myself in several long and boring afternoons. – kizzx2 Jan 29 '12 at 05:42