__block BOOL myBool = NO;
__strong MyClass *ptr = self;
self.footer.defaultSelectedItem.selectionBlock = ^{
myBool = YES;
ptr = nil;
};
This works just fine when my Scheme’s Build Configuration is set to Debug, but I get an EXC_BAD_ACCESS when I run with Release. The EXC_BAD_ACCESS happens on the the following line
if(selectionBlock != nil) selectionBlock();
but if I comment out all the lines inside my selectionBlock then it runs with no error. Why does this run in Debug and not Release?
FYI I’m using Automatic Reference Counting (arc)
** EDIT in response to @RobNapier **
The original code that works in debug is:
__block BOOL flag = NO;
__strong EventsView *ptr = self;
self.footer.defaultSelectedItem.selectionBlock = ^{
if(flag) return;
flag = YES;
[ptr backTUI:nil];
flag = NO;
};
For the life of me I could not get this to work with a block. So instead I moved to using a setter for the Event View pointer. I’m guessing that fixed the problem with my pointer by setting up an additional ARC retain. I’m still uncertain because I never saw a zombie get logged when using the block so yeah.
When in doubt, use a selector.