2010-05-19 86 views
0

我有一个Cocos2D iPhone应用程序,需要一组CGRect覆盖在图像上来检测它们中的触摸。下面的“数据”是一个保存从XML文件解析的值的类。 “delegateEntries”是一个NSMutableArray,包含多个“数据”对象,从另一个名为“条目”的NSMutableArray中提取,该条目驻留在应用程序委托中。全局NSMutableArray似乎没有价值

由于一些奇怪的原因,我可以在init函数中没有问题地获得这些值,但是进一步向下查询类,我尝试获取这些值,并且应用程序崩溃而没有错误消息(作为示例,我放入了通过“populateFieldsForTouchedItem”方法访问这些数据的“ccTouchBegan”方法)。

任何人都可以看到为什么这些值不能从其他方法访问?在dealloc之前没有对象被释放。提前致谢!

@synthesize clicked, delegate, data, image, blurImage, normalImage, arrayOfRects, delegateEntries; 
- (id)initWithTexture:(CCTexture2D *)aTexture { 

    if((self=[super initWithTexture:aTexture])) { 
     arrayOfRects = [[NSMutableArray alloc] init]; 
     delegateEntries = [[NSMutableArray alloc] init]; 
     delegate = (InteractivePIAppDelegate *)[[UIApplication sharedApplication] delegate]; 
     delegateEntries = [delegate entries]; 
     data = [delegateEntries objectAtIndex:0]; 
     NSLog(@"Assigning %@", [[delegateEntries objectAtIndex:0] backgroundImage]); 
     NSLog(@"%@ is the string", [[data sections] objectAtIndex:0]); 
     //CGRect rect; 
     NSLog(@"Count of array is %i", [delegateEntries count]); 

     //collect as many items as there are XML entries 
     for(int i=0; i<[delegateEntries count]; i++) { 
      if([[delegateEntries objectAtIndex:i] xPos]) { 
       NSLog(@"Found %i items", i+1); 
       [arrayOfRects addObject:[NSValue valueWithCGRect:CGRectMake([[[delegateEntries objectAtIndex:i] xPos] floatValue], [[[delegateEntries objectAtIndex:i] yPos] floatValue], [[[delegateEntries objectAtIndex:i] xBounds] floatValue], [[[delegateEntries objectAtIndex:i] yBounds] floatValue])]]; 
      } else { 
       NSLog(@"Nothing"); 
      } 
     } 
     blurImage = [[NSString alloc] initWithString:[data backgroundBlur]]; 
     NSLog(@"5"); 
     normalImage = [[NSString alloc] initWithString:[data backgroundImage]]; 
     clicked = NO; 
    } 
    return self;  
} 

然后:

- (void)populateFieldsForTouchedItem:(TouchedRect)touchInfo 
{ 
    Data *touchDatum = [[Data alloc] init]; 
    touchDatum = [[self delegateEntries] objectAtIndex:touchInfo.recordNumber]; 
    NSLog(@"Assigning %@", [[[self delegateEntries] objectAtIndex:touchInfo.recordNumber] backgroundImage]); 
    rect = [[arrayOfRects objectAtIndex:touchInfo.recordNumber] CGRectValue]; 
    image = [[NSString alloc] initWithString:[[touchDatum sections] objectAtIndex:0]]; 
    [touchDatum release]; 
} 

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 
    TouchedRect touchInfo = [self containsTouchLocation:touch]; 
    NSLog(@"Information pertains to %i", touchInfo.recordNumber); 
    if (!touchInfo.touched && !clicked) { //needed since the touch location changes when zoomed 
     NSLog(@"NOPE"); 
     return NO; 
    } 
    [self populateFieldsForTouchedItem:touchInfo]; 
    NSLog(@"YEP"); 
    return YES; 
} 

回答

1

它不是一个数组,它保存从代理的entries中抽取的值 - 它是完全相同的数组。您正在分配delegateEntries以指向代表的entries阵列。这意味着如果代理释放它的数组,你正在访问一个释放的对象,并且你得到了你看到的结果。如果您想要委托人阵列的副本,请执行[[delegate entries] mutableCopy]

顺便说一句,delegateEntries = [[NSMutableArray alloc] init]赋值只是一个内存泄漏,如果你正在做的立即分配变量指向委托的数组。你正在创建一个数组,不会释放它,只是在下一步忘记它。

+0

谢谢查克!这个和ccTouchBegan运行后保留“touchDatum”的结合解决了我的问题。我会进一步测试以确保,但看起来不错。感谢您的洞察! – diatrevolo 2010-05-19 18:10:04

2

改变了...

delegateEntries = [[NSMutableArray alloc] init]; delegateEntries = [delegate entries];

要这个......

delegateEntries = [[delegate entries] copy];

您目前正在泄漏内存。完成后释放委托条目。

+0

共享委托的NSMutableArray仍然可能不是一个好主意。两个对象共享可变状态是很危险的。 – Chuck 2010-05-19 18:04:25

+0

谢谢迪伦的洞察力。上面我最终使用了查克的解决方案,但是感谢你指出了这一点。如果我有足够的声望,我会为你们两个投票。 – diatrevolo 2010-05-19 18:14:42