0

私の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];
}

理由を説明できますか?

4

3 に答える 3

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];
于 2012-07-24T09:36:38.040 に答える
1

オブジェクトを init に割り当てた場合、そのオブジェクトの保持カウントは既に 1 に設定されているため、通常は保持する必要はありません。最初のコード例 ( [valueString release];) の最後でそれを解放すると、init alloc の後に保持しているため、保持カウントが 1 になります。

メモリ管理に関するパラメーターをどのようCCSequenceCCCallFuncND処理するかはわかりませんが、指定された行から保持を削除すると安全なはずです。

お役に立てれば。

于 2012-07-24T09:29:26.683 に答える
1
valueString = [[[NSString alloc] initWithString:[NSString stringWithFormat:@"%@: %@",kGameTimerus,[allFunctions getTimeFormat:(int) _timeLimit]]] retain];

ここで2回保持しました:割り当てと保持。そして、あなたは一度だけ解放します:

[valueString release];

そのため、リークの可能性があります (実際にはリークです)。

そして、

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

一度保持 (割り当て) し、不要になったら解放 ( autorelease) します。valueStringこれは大丈夫です。

于 2012-07-24T09:30:48.107 に答える