- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个每秒触发一次的 NSTimer。
而且每一秒我都有一个需要更改的 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 /:
}
最佳答案
您为每个对象分配了两次内存。当您第二次分配并将其分配给同一个变量时,第一 block 分配的内存变得不可访问和不可释放。
然后你制作一个 testedit 的可变副本,并将副本分配给原始变量。同样,您留下了一 block 不可访问的内存。
非 ARC 内存管理的规则是 - 对于每个 alloc、new、copy 或 retain您需要有相应的版本。你有 6 个分配,一个副本,只有 3 个发布。
这里有一些建议。
删除这些重复的分配:
testedit = [[NSString alloc] init];
withString1a = [[NSString alloc] init];
forString1a = [[NSString alloc] init];
大概testedit
、withString1a
和forString1a
都是iVar。 (Please declare your iVars as autosynthesized properties 并将它们称为 self.testedit ... 等等,这将使您的代码对于堆栈溢出者来说更加清晰)。
取出所有这些:
if (testedit) {
[testedit release];
[withString1a release];
[forString1a release];
}
假设这些都是 iVar,释放它们的正确位置是在对象的 dealloc
方法中
事实上 withString1a
和 forString1a
可以是局部变量,因为您可以从其他地方获取它们的内容:
NSString* withString1a = [[[NSString alloc] initWithFormat:@"e"] autorelease];
NSString* forString1a = [[[NSString alloc] initWithFormat:@"flk34j"] autorelease];
您可以自动释放
它们,因为您不需要它们在方法完成后停留。
这些行也可以写成:
NSString* withString1a = [NSString stringWithFormat:@"e"];
NSString* forString1a = [NSString stringWithFormat:@"flk34j"];
(-stringWithFormat 是一种返回自动释放对象的便捷方法)
这给我们留下了这两行。
testedit = [[NSString alloc] initWithFormat:@"example"];
testedit = [[testedit stringByReplacingOccurrencesOfString:withString1a
withString:forString1a] mutableCopy];
不清楚为什么在第一行将 testedit 视为不可变字符串,在第二行将其视为可变字符串。您在这里根本不需要可变字符串,因为您正在用新字符串替换 testedit
。
self.testedit = [[NSString alloc] initWithFormat:@"example"];
self.testedit = [[testedit stringByReplacingOccurrencesOfString:withString1a
withString:forString1a] copy];
(您需要复制
,因为stringByReplacingOccurrencesOfString:withString:
返回一个自动释放的对象,在这里您要保留它)
拼图的最后一 block 是摆脱你的 _testedit iVar 内存分配。您可以在对象的 dealloc
方法中执行此操作:
- (void) dealloc {
[_testEdit release];
[super dealloc];
}
(请注意,init
、accessor 和 dealloc
方法是您不应该使用属性语法引用 iVar 的三个地方。 )
很好,但实际上,您应该使用 ARC!与依赖编译器为您管理内存相比,以这种方式引入内存错误的可能性要大得多。
关于iphone - MutableCopy AllocLeak 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14469258/
我有一个每秒触发一次的 NSTimer。 而且每一秒我都有一个需要更改的 NSString。 我以前从未尝试过处理内存管理,所以我不确定我在做什么是否正确,但仪器在“alloc”下说 stringBy
我是一名优秀的程序员,十分优秀!