Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properly defining a static NSString* c-style array under ARC

In my implementation file, I have a static 2-dimensional c-style array of NSString* defined as:

static NSString* TheColorArray[][3] = {
    [RedType] = {@"red", @"blah", @"YES"},
    [BlueType] = {@"blue", @"yadda", @"YES"},
    .....
}

The method in question accesses the array like:

-(NSString*)value:(NSInteger)value {
    return TheColorArray[value][0];
}

This all seems to work fine about 99% of the time...but when it fails to work it always fails with EXC_BAD_ACCESS - KERN_INVALID_ADDRESS at 0x11

I've verified that my value parameter is not beyond the bounds of the array. It seems odd that the address is 0x11...which kind of implies that the array has not been initialized.

So what is happening here? Is there some "gotcha" I need to be aware of with ARC and c-style arrays?

like image 816
Tim Reddy Avatar asked May 17 '26 01:05

Tim Reddy


1 Answers

The documentation is clear that structs cannot contain ARC'd objects, we could argue that this applies to multidimensional array datums as well, though it doesn't say so. Did you try this:

static NSString __unsafe_unretained * TheColorArray[][3] = ...

This will take the objects allocated here out of ARC and remove any possibility that the compiler is aggressively nulling where it shouldn't be.

Assuming that's the problem. Maybe if you're able to step back with the debugger, can you see what symbol is being dereferenced when it errs?

like image 59
iluvcapra Avatar answered May 18 '26 15:05

iluvcapra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!