iphone:如何解决这个内存泄漏?

发布于 2024-12-08 22:56:45 字数 562 浏览 6 评论 0原文

如何解决这个内存泄漏...我什至在最后释放它,如图所示,但它仍然存在。在 if 语句中,几乎 10-15 条件它的使用类似于给定的代码......但最后我释放了它。

在此处输入图像描述

LoginResponse *response = [[LoginResponse alloc] initWithMessageString: messageString];


ServerMessage *ackMessage = [[ServerMessage alloc] initWithMessageToAck:response];
[[NSNotificationCenter defaultCenter] postNotificationName:@"SendMessageToServer" object:ackMessage];

[[NSNotificationCenter defaultCenter] postNotificationName:@"LoginResponseReceived" object:response];

How to solbe this memory leak ... I even release it at the end as in the pic but its still there. In if statment almost 10-15 condition its using like the given code... But at the end I release it.

enter image description here

LoginResponse *response = [[LoginResponse alloc] initWithMessageString: messageString];


ServerMessage *ackMessage = [[ServerMessage alloc] initWithMessageToAck:response];
[[NSNotificationCenter defaultCenter] postNotificationName:@"SendMessageToServer" object:ackMessage];

[[NSNotificationCenter defaultCenter] postNotificationName:@"LoginResponseReceived" object:response];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初吻给了烟 2024-12-15 22:56:45

没有释放messageString。您正在做的是:

// there's a messageString
if(...){
     NSString* messageString= [[NSString alloc] init ... ]
                                   // you're declaring new messageString, 
                                   // not related to the outer messageString
     ...
     // and you didn't release the inner messageString. 
     // The pointer messageString just goes away.
}
[messageString release]; // you're releasing outer messageString, not inner messageString.

从 XCode 执行“分析”。 (它位于“build”菜单项下方。)我认为应该解决忘记释放内部 messageString 的问题。在运行 Instruments 之前使用“分析”。

You're not releasing messageString. What you're doing is this:

// there's a messageString
if(...){
     NSString* messageString= [[NSString alloc] init ... ]
                                   // you're declaring new messageString, 
                                   // not related to the outer messageString
     ...
     // and you didn't release the inner messageString. 
     // The pointer messageString just goes away.
}
[messageString release]; // you're releasing outer messageString, not inner messageString.

Perform "Analyze" from XCode. (It's below the "build" menu item.) I think that should capture this problem of forgetting to release the inner messageString. Use "Analyze" before running Instruments.

梦断已成空 2024-12-15 22:56:45

看看您是否将其保留在代码中的其他位置。如果是这样,可能需要额外的版本。另请注意,您可能使用的传递 messageString 作为参数的方法也可能会保留它。

See if you are retaining it somewhere else in the code. If so, that might require an extra release. Also note that a method that you might be using passing messageString as argument might also be retaining it.

绅刃 2024-12-15 22:56:45

确保释放 if 块内的字符串。

Make sure to release the string inside the if block.

风向决定发型 2024-12-15 22:56:45

基本的经验法则是,对于每个 allocnewretaincopy,您都需要一个 <代码>释放或<代码>自动释放。您似乎在某处缺少 releaseautorelease

顺便说一句,在部署到测试设备之前,您可以(并且应该)使用 Xcode 的“构建和分析”来帮助查找内存泄漏。

The basic rule of thumb is that for every alloc, new, retain, or copy, you need a release or autorelease. It seems that you are missing a releaseor autorelease somewhere.

By the way, you cam (and should) use Xcode's "Build and Analyze" to help find memory leaks before you even deploy to a testing device.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文