Take the 2-minute tour ×

I have seen a lot of answers around, but I couldn't find any solution for my problem.

I basically have a Class where I only use class methods. I never allocate this class. So, I am passing a block to it and storing it on a static on the .h of the class, like this:

static ErrorBlock _errorBlock;

I am storing this way:

_errorBlock = [errorBlock copy];

I receive errorBlock as a parameter of a method. After some calculations, I invoke the block like this:


It's worth saying that I am invoking this from a Class's Category. The application basically returns:

EXC_BAD_ACCESSS(code=2, adress = 0xc)

When I check the _errorBlock value, it's nil. So my question is, how can I keep a live reference to the block?

share|improve this question
add comment

2 Answers

If _errorBlock is "nil" it might be safe to say that it was assigned with nil and there is no reference to keep it alive. If you create a reference as in NSObject *reference = someObject and someObject is released your reference pointer will still point to location where someObject used to be and should not be nil. Check if you have some problems with assigning the block or if it even gets called. Also you might want to check if _errorBlock exists before calling it.

share|improve this answer
When this _errorBlock = [errorBlock copy];happens, both _erroBlock and erroBlock are not nil. –  Jacky Boy Nov 5 '12 at 15:45
Strange. Something must be setting it to nil.. Does anything else use this block? Can you call it directly after assignment? (in the same method) –  Matic Oblak Nov 5 '12 at 15:48
Nothing else is calling it. I have done a project search, just to be sure. –  Jacky Boy Nov 5 '12 at 15:49
I opened a new project and this code works for me..: static void (^doStuff)(int a); + (void)setDoStuff:(void (^)(int))inputN { doStuff = [inputN copy]; } + (void)performDo { doStuff(0); } - (void)viewDidLoad { [ViewController setDoStuff:^(int a) { NSLog(@"%d", a); }]; [super viewDidLoad]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [ViewController performDo]; } –  Matic Oblak Nov 6 '12 at 8:10
add comment
up vote 0 down vote accepted

I was able to work it out, my removing completely the use of global blocks. At the moment I use the blocks my passing them as parameters.

share|improve this answer
add comment

Your Answer


