【发布时间】: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