这里的内存泄漏在哪里?
仪器告诉我这段代码中存在内存泄漏,但我似乎找不到它......有什么帮助吗? 抱歉或新手问题。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
int altoBufferCelda = 26;
Mensaje *msg = (Mensaje *)[model.mensajes objectAtIndex:indexPath.row];
CGSize txtSize = [msg.texto sizeWithFont:[UIFont systemFontOfSize:17.0f] constrainedToSize:CGSizeMake(222, 222) lineBreakMode:UILineBreakModeTailTruncation];
[alturasDinamicas setObject:[NSNumber numberWithFloat:(txtSize.height + altoBufferCelda)] forKey:[NSNumber numberWithInt:indexPath.row]];
return txtSize.height + altoBufferCelda;
}
Instruments tells me there's a mem leak in this code, but I can't seem to find it....any help? sorry or the newbie question.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
int altoBufferCelda = 26;
Mensaje *msg = (Mensaje *)[model.mensajes objectAtIndex:indexPath.row];
CGSize txtSize = [msg.texto sizeWithFont:[UIFont systemFontOfSize:17.0f] constrainedToSize:CGSizeMake(222, 222) lineBreakMode:UILineBreakModeTailTruncation];
[alturasDinamicas setObject:[NSNumber numberWithFloat:(txtSize.height + altoBufferCelda)] forKey:[NSNumber numberWithInt:indexPath.row]];
return txtSize.height + altoBufferCelda;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
<罢工>
我会说:
[NSNumber numberWithFloat]
它将为您分配一个自动释放的对象。 iPhone 不会进行垃圾收集,只会收集引用。 由于您在离开方法之前没有释放正在分配的内存,因此 Instruments 将其报告为泄漏。由于目前已接受这一点,因此我将更改我的答案。
乐器并不是神圣的法令。 这可能是错误的。 使用它作为您应该查看的内容的强有力指南,但如果您确实找不到代码中的任何错误或漏洞,请继续前进。
I would say:
[NSNumber numberWithFloat]
It will allocate an autoreleased object for you. The iPhone isn't garbage collected, just reference collected. And since you aren't releasing the memory you are allocating before you leave the method, Instruments is reporting it as a leak.Since this is currently accepted, I'll kind of change my answer.
Instruments isn't a divine edict. It could be wrong. Use it as a strong guideline of what you should be looking at, but if you honestly cannot find anything wrong or leaky with the code, just move on.
我在你的代码中看不到任何内存泄漏。 正如 Toast 指出的那样,Instruments 并不总是准确的。 这主要是因为即使是 Apple Framework 中的代码也包含内存泄漏,Instruments 也报告了这些问题。
如果您使用的是 XCode 3.2,您可以从“构建”菜单中选择“构建和分析”,该菜单会扫描代码以查找编译器通常无法检测到的错误。 这将向您展示许多由于忘记释放对象而可能导致的内存泄漏。
I cannot see any memory leak in your code. As toast points out, Instruments isn't always accurate. This is mostly because even the code from the Apple Frameworks contains memory leaks, which are reported by Instruments too.
If you are using XCode 3.2 you can choose Build and Analyze from the Build menu which scans your code for errors normally undetected by the compiler. This will show you many possible memory leaks resulting from forgetting to release an object.