I’m seeing some rather strange behavior as I’m debugging my iPhone project. Among other things, in the header file for a UIViewController descendant, I have this:
@interface OOGameDetailsViewController : UIViewController <OOXMLParserDelegate,OOServerDelegate>
{
...
OOXMLNode *node;
float elheights[6];
...
}
@property (readwrite, retain) OOXMLNode *gameNode;
...
@end
Then in the code, I’m assigning values to this array:
- (void)setNode:(OOXMLNode *)aNode
{
node = aNode;
[self updateUserDefaults];
[background setImage:[gameNode imageFromKey:@"background"]];
CGFloat maxw = 240.0f;
NSString *text = [gameNode elementNodeWithKey:@"title"].value;
CGSize sizeOfText=[text sizeWithFont:[UIFont boldSystemFontOfSize:20.0f]
constrainedToSize:CGSizeMake(maxw, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
...
elheights[0] = 10 + sizeOfText.height;
text = [gameNode elementNodeWithKey:@"age"].value;
sizeOfText=[text sizeWithFont:[UIFont systemFontOfSize:16.0f]
constrainedToSize:CGSizeMake(maxw, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
...
elheights[1] = 10 + sizeOfText.height;
...
elheights[2] = 10 + height;
...
elheights[3] = 10 + separatorHeight;
text = [gameNode elementNodeWithKey:@"intro"].value;
sizeOfText=[text sizeWithFont:[UIFont boldSystemFontOfSize:14.0f]
constrainedToSize:CGSizeMake(maxw, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
...
elheights[4] = sizeOfText.height;
...
elheights[5] = 20 + btnHeight;
[mainTable reloadData];
}
The ideas is quite simple: I’m assigning 6 floating point values to a 6-element array – with indices from 0 to 5. However as I’m stepping through this code in the debugger, I consistently see each of the assignments assign the element of the array with index 1 higher than indicated. That is, elheights[0] = ... actually assigns this vlaue into elheights[1] and so on. The last assignment (assigning to element with index 5 simply does nothing).
To prove that I’m not crazy, here are screenshots of the debugger (crops of the relevant part – to keep image size to minimum). The first one is right before the assignment, the second one is right after the assignment.


I cleaned the project and restarted; I closed the simulator, closed Xcode, reopened and restarted – same results. Is there something special about the way ObjectiveC handles simple C-style arrays?
Thanks in advance,
-Aleks
XCode4 uses new LLDB debugger by default, which does not always work perfectly yet. (For example, the current version does not recognize structure alignment attribute). It also has some bugs here and there.
Switch to GDB (click Scheme / Edit Scheme in the toolbar, there is the “Debugger” popup button with two choices) if you’re having problems with it.