I’m getting EXC_BAD_ACCESS crashes when assigning a value in an id __strong * array
Here’s the code
id __strong *entries;
entries = (id __strong *)malloc(sizeof(id) * 20);
for (NSUInteger j = 0; j < 20; j++)
{
entries[j] = @{@"key1" : @"value1", // Crash
@"key2" : @"value2",
@"key3" : @"value3"]};
}
//...
free(entries);
It does not matter what the value is. Even this:
entries[j] = [NSNumber numberWithInt:1];
crashes.
It does not crash on every time, but it happens within a few tries. Crashes happen on assigning a value at index 0, so it is not something that happens half way through a for loop and cutting out the for loop does not fix the crash.
Enabling NSZombies stops appears the crash from happening but there is no output complaining about any Zombies. The same happens when using the Zombies Instrument – no crash, no zombie output. Enabling Guard Malloc also seems to stop the crash.
Changing the __strong to __autoreleasing seems to also stop the crash but is this really the fix for the problem and if so, why?
Any ideas?
Im Not 100% sure, but shouldn’t you use calloc?
Maybe that’s the problem because it isn’t zero initialised. Before freeing, you need to nil the variables.