2012-07-24 104 views
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]; 
} 

你能解釋一下爲什麼嗎?

+0

在allFunctions.m中,您應該重新排列字符串。但是,在這些情況下,我會放棄字符串。使用autorelease而不是release,不要釋放它。此外(但shoudl與警告不相關)我會將535行移入if ... rus語句的else分支。 – 2012-07-24 09:31:23

+0

對不起,在allFunctions.m你已經自動發佈它。我忽略了這一點。這沒關係。 – 2012-07-24 09:37:39

回答

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。這沒關係。

+0

非常感謝!有幫助 – 2012-07-24 09:51:22

+0

@AlexanderSharunov歡迎你!順便說一句,如果你不熟悉內存管理,你可以嘗試使用[ARC](http://developer.apple.com/library/mac/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html)。 :) – Kjuly 2012-07-24 09:55:21

+0

好的,我會試試,對不起我的英文 – 2012-07-24 16:24:18

1

當您將init分配給一個對象時,該對象已將保留計數設置爲1,因此通常不需要保留它。當您在第一個代碼示例([valueString release];)結束時釋放它時,它將保留計數爲1,因爲您在init alloc之後保留了它。

我不確定CCSequenceCCCallFuncND如何處理有關內存管理的參數,但是如果從指定行中刪除了保留,則應該是安全的。

希望這會有所幫助。

+0

非常感謝!有幫助 – 2012-07-24 09:51:55

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];