全局NSMutableArray似乎没有持有值

时间:2010-05-19 17:58:12

标签: objective-c cocoa-touch cocos2d-iphone

我有一个Cocos2D iPhone应用程序,它需要在图像上叠加一组CGRect来检测其中的触摸。下面的“数据”是一个包含从XML文件解析的值的类。 “delegateEntries”是一个NSMutableArray,它包含几个“数据”对象,从另一个名为“entries”的NSMutableArray中提取,这些对象位于应用程序委托中。

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

任何人都可以看到为什么这些值无法通过其他方法访问?在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;
}

2 个答案:

答案 0 :(得分:2)

改变这个......

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

对此...

delegateEntries = [[delegate entries] copy];

您目前正在泄露记忆。完成后释放delegateEntries。

答案 1 :(得分:1)

它不是一个数组,用于保存从代理entries中提取的值 - 它是完全相同的数组。您指定delegateEntries指向代理的entries数组。这意味着如果委托释放其数组,您将访问一个释放的对象,并获得您所看到的结果。如果您需要委托数组的副本,请执行[[delegate entries] mutableCopy]

顺便提一下,delegateEntries = [[NSMutableArray alloc] init]赋值只是一个内存泄漏,如果您要立即指定变量指向委托的数组。你正在那条线上创建一个数组,永远不会释放它,只是在下一步中忘记它。