Reputation: 3543
In this code, in the second for loop, i get an EXC_BAD_ACCESS if I use an identifier other than i and j.
if (UIDeviceOrientationIsPortrait(deviceOrientation)){
numRows = 4;
numCols = 3;
}else {
numRows = 6;
numCols = 1;
}
for (int row = 0; row < numRows; row++){
for (int col = 0; col < numCols; col++) {
keysArray[row][col] = [[[keys objectAtIndex:0] retain] autorelease];
if (col < numRows)
[keys removeObjectAtIndex:0];
}
}
//Here is the crash
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++){
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
b.frame = CGRectMake(i * kKeyGap, j * kKeyGap, 57, 57);
[b setImage: [UIImage imageNamed:keysArray[j][i]]
forState:UIControlStateNormal];
[self.view addSubview:b];
}
}
Why would this cause such an error? I tried using Edit All In Scope to avoid missing one, but it still crashes.
Thanks
Upvotes: 0
Views: 326
Reputation: 70753
In your first loop you are referencing keysArray[row][col]
in the second loop you seem to be using keysArray[col][row]
which would cause it to crash if numRows and numCols are different
Upvotes: 0
Reputation: 3708
Your line:
keysArray[row][col] = [[[keys objectAtIndex:0] retain] autorelease];
I'm not an Objective-C expert or anything, but I'm not certain you need to autorelease that object because you haven't allocated memory for it. What happens when you try and remove that autorelease?
My suspicion is that after you autorelease the object and then attempt to assign the array value here:
[b setImage: [UIImage imageNamed:keysArray[j][i]]
forState:UIControlStateNormal];
you're getting the EXC_BAD_ACCESS.
I could be wrong, in which case I hope someone smarter than me can clear this up. :)
Upvotes: 1