XCode Analyzer:对象的潜在泄漏

Max*_*Max 1 xcode memory-leaks memory-management objective-c 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);
}
Run Code Online (Sandbox Code Playgroud)

分析员说"对象的潜在泄漏"在线:

NSString *spriteName0 = [NSString stringWithFormat:@"%@%i.png", sN, 0];
Run Code Online (Sandbox Code Playgroud)

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

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

self.name = [playersNames objectAtIndex:type];
Run Code Online (Sandbox Code Playgroud)

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

谢谢你的帮助

Ext*_*ire 5

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

self.arrayPages = [[NSMutableArray alloc] initWithCapacity:self.numPages];
Run Code Online (Sandbox Code Playgroud)

如果arrayPages保留,则执行此操作:

self.arrayPages = [[[NSMutableArray alloc] initWithCapacity:self.numPages] autorelease];
Run Code Online (Sandbox Code Playgroud)

或更简单地说:

self.arrayPages = [NSMutableArray arrayWithCapacity:self.numPages];
Run Code Online (Sandbox Code Playgroud)

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