iphone:如何解决这个内存泄漏?
如何解决这个内存泄漏...我什至在最后释放它,如图所示,但它仍然存在。在 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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您没有释放
messageString
。您正在做的是:从 XCode 执行“分析”。 (它位于“build”菜单项下方。)我认为应该解决忘记释放内部
messageString
的问题。在运行 Instruments 之前使用“分析”。You're not releasing
messageString
. What you're doing is this: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.看看您是否将其保留在代码中的其他位置。如果是这样,可能需要额外的版本。另请注意,您可能使用的传递
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.确保释放
if
块内的字符串。Make sure to release the string inside the
if
block.基本的经验法则是,对于每个
alloc
、new
、retain
或copy
,您都需要一个 <代码>释放或<代码>自动释放。您似乎在某处缺少release
或autorelease
。顺便说一句,在部署到测试设备之前,您可以(并且应该)使用 Xcode 的“构建和分析”来帮助查找内存泄漏。
The basic rule of thumb is that for every
alloc
,new
,retain
, orcopy
, you need arelease
orautorelease
. It seems that you are missing arelease
orautorelease
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.