在objectAtIndex之后NSMutablearray无效

时间:2009-11-16 15:46:17

标签: iphone cocoa cocos2d-iphone nsmutablearray

@implementation Level
@synthesize doors, rooms;
- (id) init
{
   self = [super init];
   if (self != nil) {
      rooms = [[NSMutableArray alloc] init];
      doors = [[NSMutableArray alloc] init];
   }
   return self;
}


- (void)generate{
   int room_count = 2;
   Room *room; 
   for (int i=0; i<room_count; i++) {
     room = [[Room alloc] init];
     [room generate_Doors];
     [self.rooms addObject:room];
     [room release];
  }
  for (int i=0; i<[rooms count]; i++) {
    Room *r=[rooms objectAtIndex:i];
    //After this point rooms is invalid
    int l=[[r doors] count];
    for (int j=0; j<l; j++) {
        Door *d=[[[rooms objectAtIndex:i] doors] objectAtIndex:j];
        [self.doors addObject:d];
    }
  }

}

这是我在调试器中看到的

alt text http://img163.imageshack.us/img163/8090/20091117174111.png

2 个答案:

答案 0 :(得分:0)

您显示的调试片段并不表示房间无效,只是它没有显示您期望的值 - 如果您在循环后停在调试器中并在调试器控制台中键入:

po rooms

它显示什么?

你的代码看起来不错。

答案 1 :(得分:0)

调试窗口有时会这样做。如果不是“超​​出范围”。

你可以做的不多,这是XCode中的一个小错误。仅仅因为调试器无法显示窗口中的值,并不意味着对象已经消失。

请阅读此stackoverflow.com question about debugging。它有一些非常好的调试信息。好消息要知道!