My app is recently crashing randomly reporting a EXC_BAD_ACCESS during the execution of one of the core function of the app itself, the bug has always been there but it keeps happening a lot more after the introduction of iOS 9.3.1.
I've been doing a lot of analysis and static inspection of the code using the tools made available by XCode (Code Analysis, NSZombies, Address Sanitizer and so on). Before proceeding with further investigations I wanted to make sure the count of warnings and potential memory errors is down to 0.
Right now I'm stuck with this warning "Capturing 'endblock' strongly in this block is likely to lead to a retain cycle". I really think that solving this warning could solve also the EXC_BAD_ACCESS problem as using the Leaks instrument the piece of code labeled with that warning is causing a lot of leaks.
Here's the code snippet of the function causing the leaks:
- (void)arrayDayPlan:(void (^)()) block dateArray:(NSArray *)dateArray {
[MyPlanner sharedInstance].multipleDaycounter = 1;
NSInteger dayNumber = dateArray.count;
void (^__block endBlock)() = ^void() {
if([MyPlanner sharedInstance].multipleDaycounter == dayNumber) {
block();
} else {
NSDate *newDate = [dateArray objectAtIndex:[MyPlanner sharedInstance].multipleDaycounter];
[MyPlanner sharedInstance].multipleDaycounter = [MyPlanner sharedInstance].multipleDaycounter + 1;
[[MyPlanner sharedInstance] plan:endBlock FromDay:[newDate dateAtStartOfDay] toDay:[[newDate dateByAddingDays:1] dateAtStartOfDay]];
}
};
NSDate *firstDate = (NSDate *)[dateArray firstObject];
[[MyPlanner sharedInstance] plan:endBlock FromDay:[firstDate dateAtStartOfDay] toDay:[[firstDate dateByAddingDays:1] dateAtStartOfDay]];
}
The line where I get that warning is this one inside the block:
[[MyPlanner sharedInstance] plan:endBlock FromDay:[newDate dateAtStartOfDay] toDay:[[newDate dateByAddingDays:1] dateAtStartOfDay]];
While the leaks are reported for this line:
void (^__block endBlock)() = ^void() {
This function is called in four places around the project and each call has more or less the following structure:
- (void)planWithArray {
[self showLoadingView];
__block MyAssignedViewController *blockSafeSelf = self;
[[MyPlanner sharedInstance] arrayDayPlan:^{
[[MyPlanningData sharedInstance] resetDateToPlan];
[blockSafeSelf refreshView];
[blockSafeSelf dismissLoadingView];
} dateArray:[[MyPlanningData sharedInstance] generateDateArray:[[NSDate now] dateAtStartOfDay]]];
}
I've read on SO (here, here and on so many other threads) that others had similar problem but with usage of self variable. Does it apply even on my case?
I've also read that using a __weak reference could save my from this problem. Should I change the blockSafeSelf definition from __block to __weak?
Thanks in advance!