Sco*_*ker 0 memory cocoa-touch memory-leaks objective-c nsmutablearray
我在释放和泄漏方面遇到了麻烦.我有一个不会停止泄漏的阵列!这是我的代码:我已经在.h中声明了otherValuesArray我尝试了数百种不同的方式,包括autorelease.
谁能告诉我我做错了什么?谢谢
otherValuesArray = [[NSMutableArray array] retain]; //89% leak
NSString *tempString;
tempString = [[NSString stringWithFormat:@"%d",challengeID] autorelease];
[otherValuesArray addObject:[NSString stringWithString:tempString]]; // 11% leak
tempString=nil;
tempString = [[NSString stringWithFormat:@"%d",scoreMultiQuant] autorelease];
[otherValuesArray addObject:[NSString stringWithString:tempString]];
tempString=nil;
int challengeDoneTemp = [challenges otherValues:otherValuesArray];
tempString=nil;
[tempString release];
otherValuesArray = nil;
[otherValuesArray release];
Run Code Online (Sandbox Code Playgroud)
交换最后两行.设置otherValuesArray
为nil后,发送释放消息毫无意义.它已经是零,所以发布没有效果.所以你正在泄漏那些记忆,因为它没有被释放.正确的代码将是,
[otherValuesArray release]; // first release otherValuesArray = nil; // then set to nil
而且还stringWithFormat
已经自动释放了.所以你不需要发送自动发布消息.当容器泄漏时,您正在泄漏它otherValuesArray
.
而且(虽然与泄漏无关)你根本不需要tempString.您可以在一行中执行此操作:
[otherValuesArray addObject:[NSString stringWithFormat:@"%d",challengeID]];
归档时间: |
|
查看次数: |
135 次 |
最近记录: |