【问题标题】:Potential leak of an object allocated分配的对象的潜在泄漏
【发布时间】:2012-07-24 09:25:12
【问题描述】:

分析我的 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];
}

你能解释一下为什么吗?

【问题讨论】:

  • 在 allFunctions.m 你应该释放字符串。但是,在这些情况下,我会 aurorelease 字符串。使用 autorelease 而不是 release 并且不要释放它。另外(但不应该与警告相关)我会将第 535 行移到 if ... rus 语句的 else 分支中。
  • 对不起,你已经在 allFunctions.m 中自动发布了它。我忽略了这一点。没关系。

标签: ios memory-leaks cocos2d-iphone


【解决方案1】:
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];

【讨论】:

    【解决方案2】:
    valueString = [[[NSString alloc] initWithString:[NSString stringWithFormat:@"%@: %@",kGameTimerus,[allFunctions getTimeFormat:(int) _timeLimit]]] retain];
    

    您在这里保留了两次:分配和保留。然后你只释放一次:

    [valueString release];
    

    这就是为什么存在潜在泄漏(实际上是泄漏)。

    对于

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

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

    【讨论】:

    • @AlexanderSharunov 不客气!顺便说一句,如果您不熟悉内存管理,可以尝试使用ARC。 :)
    【解决方案3】:

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

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

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-05
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多