分配的对象可能泄漏

时间:2012-07-24 09:25:12

标签: ios memory-leaks cocos2d-iphone

分析我的cocos2d游戏后,我收到一条警告“在第525行分配的对象可能泄漏并存入'valueString'”此代码中

525  NSString * valueString=[[[NSString alloc] initWithString:[NSString stringWithFormat:@"%@: %@",kGameTimeeng,[allFunctions getTimeFormat:(int) _timeLimit]]] retain];

    if([_language isEqualToString:@"rus"]){
        [valueString release];
        valueString=[[[NSString alloc] initWithString:[NSString stringWithFormat:@"%@: %@",kGameTimerus,[allFunctions getTimeFormat:(int) _timeLimit]]] retain];
    }    

    id sequence=[CCSequence actions:
                 [CCCallFuncND actionWithTarget: allFunctions selector: @selector(setLabelColor:withIndex:) data:(void*)color],
                 [CCCallFuncND actionWithTarget: allFunctions selector: @selector(setLabelValue:withValue:) data:(NSString*)valueString],
                // [CCCallFuncND actionWithTarget: self selector: @selector(setLabelStroke:withTag:) data:(void*)TagCurentPointsLabelStroke],
                 [CCBlink actionWithDuration:0.5f blinks:2],
                 [CCShow action], 
                 [CCCallFuncND actionWithTarget: allFunctions selector: @selector(setLabelColor:withIndex:) data:(void*)colorAfter],

                 nil];

    [_timeLimitLabel runAction:sequence];
    [valueString release];

allFunctions.m

-(void) setLabelValue:(id) sender withValue:(NSString*) value
{   
    CCLabelTTF *label=(CCLabelTTF *)sender;
    NSString * valueString=[[[NSString alloc] initWithString:[NSString stringWithFormat:@"%@",value]] autorelease];
    [label setString:[NSString stringWithFormat:@"%@",valueString]];
   //[valueString release];
}
你可以解释一下为什么吗?

3 个答案:

答案 0 :(得分:2)

525 if([_language isEqualToString:@"rus"]){
        [valueString release];
        valueString=[[[NSString alloc] initWithString:[NSString stringWithFormat:@"%@: %@",kGameTimerus,[allFunctions getTimeFormat:(int) _timeLimit]]] autorelease];
    } else {    
        NSString * valueString=[[[NSString alloc] initWithString:[NSString stringWithFormat:@"%@: %@",kGameTimeeng,[allFunctions getTimeFormat:(int) _timeLimit]]] autorelease];
    }


    id sequence=[CCSequence actions:
                 [CCCallFuncND actionWithTarget: allFunctions selector: @selector(setLabelColor:withIndex:) data:(void*)color],
                 [CCCallFuncND actionWithTarget: allFunctions selector: @selector(setLabelValue:withValue:) data:(NSString*)valueString],
                // [CCCallFuncND actionWithTarget: self selector: @selector(setLabelStroke:withTag:) data:(void*)TagCurentPointsLabelStroke],
                 [CCBlink actionWithDuration:0.5f blinks:2],
                 [CCShow action], 
                 [CCCallFuncND actionWithTarget: allFunctions selector: @selector(setLabelColor:withIndex:) data:(void*)colorAfter],

                 nil];

    [_timeLimitLabel runAction:sequence];

答案 1 :(得分:1)

当您为init分配一个对象时,该对象已将retain count设置为1,因此您通常不需要保留它。当您在第一个代码示例([valueString release];)结束时释放它时,它将保留计数设置为1,因为您在init alloc之后保留了它。

我不确定CCSequenceCCCallFuncND如何处理有关内存管理的参数,但如果从指定的行中删除保留,则应该是安全的。

希望这会有所帮助。

答案 2 :(得分:1)

valueString = [[[NSString alloc] initWithString:[NSString stringWithFormat:@"%@: %@",kGameTimerus,[allFunctions getTimeFormat:(int) _timeLimit]]] retain];

你在这里保留了两次:alloc&保留。然后你只发布一次:

[valueString release];

这就是潜在泄漏的原因(实际上,这是泄漏)。

NSString * valueString = [[[NSString alloc] initWithString:[NSString stringWithFormat:@"%@",value]] autorelease];

您保留一次(alloc),并在不再需要autorelease时释放(valueString)。这没关系。