XCode Analyzer:对象的潜在泄漏

时间:2013-01-25 14:53:05

标签: objective-c xcode memory-management memory-leaks cocos2d-iphone

这是一段代码,你能帮我正确管理内存吗?

- (void) buildSpritesWithName:(NSString*)sN {

    self.arrayPages = [[NSMutableArray alloc] initWithCapacity:self.numPages];
    NSString *spriteName0 = [NSString stringWithFormat:@"%@%i.png", sN, 0];
    CCSprite *temp0 = [CCSprite spriteWithSpriteFrameName:spriteName0];

    NSLog(@"temp sprite %@ size : %f / %f",spriteName0, temp0.contentSize.width, temp0.contentSize.height);

    self.imgSize = CGSizeMake(temp0.contentSize.width, temp0.contentSize.height);


    for (int c = 1; c <= numberOfWheelFacesCycles; c++) {

        for (int x = 1; x <= self.numPages; x++) {

            NSString *spriteName = [NSString stringWithFormat:@"%@%i.png",sN, x];
            CCSprite *temp = [CCSprite spriteWithSpriteFrameName:spriteName];

            [self.arrayPages addObject:temp];
        }
    }

    NSLog(@"%i Pages built",self.arrayPages.count);
}

分析仪说&#34;物体的潜在泄漏&#34;在线:

NSString *spriteName0 = [NSString stringWithFormat:@"%@%i.png", sN, 0];

为什么? NSString是autorelease对吗?哪个对象可能泄露?

我在另一个课程中还有以下内容,问题是:

self.name = [playersNames objectAtIndex:type];

顺便说一句,如果循环的管理不好,你也可以提供建议吗?

感谢您的帮助

1 个答案:

答案 0 :(得分:5)

查看arrayPages的财产或访问者。如果它保留,那么当您致电时会出现潜在的泄漏:

self.arrayPages = [[NSMutableArray alloc] initWithCapacity:self.numPages];

如果arrayPages保留,请改为执行此操作:

self.arrayPages = [[[NSMutableArray alloc] initWithCapacity:self.numPages] autorelease];

或更简单:

self.arrayPages = [NSMutableArray arrayWithCapacity:self.numPages];

至于你的循环,它看起来很好,但要小心在其中创建许多自动释放的对象;他们直到某个时候才被释放。