我有一个每秒触发一次的NSTimer。MutableCopy AllocLeak MemoryLeak
每隔一秒我有一个NSString需要改变。
我从来没有试过处理内存管理,所以我不确定我所做的是正确的,但仪器在“alloc”下面说stringByReplacingOccurrencesOfString
的代码行有45MB的“Live Bytes “大约一分钟后......
(并且活动字节计数每秒都在持续上升并最终导致应用程序崩溃)。
我认为我的问题在于某处与MutableCopy
代码?
这里是我的代码:
-(void)myTimer {
if (testedit) {
[testedit release];
[withString1a release];
[forString1a release];
}
testedit = [[NSString alloc] init];
withString1a = [[NSString alloc] init];
forString1a = [[NSString alloc] init];
testedit = [[NSString alloc] initWithFormat:@"example"];
withString1a = [[NSString alloc] initWithFormat:@"e"];//this string gets its values randomly from an array in my real code
forString1a = [[NSString alloc] initWithFormat:@"flk34j"];//this string gets its values randomly from an array in my real code
testedit = [[testedit stringByReplacingOccurrencesOfString:withString1a withString:forString1a] mutableCopy];//memory leak /:
}
你为什么要做这么多的分配?你在使用ARC吗? – iDev
不,ARC被禁用。 /: –
您是否将'testedit'声明为'@ property'还是仅仅是一个类的变量? – iDev