如何解决此内存泄漏...我什至在最后释放它,如图片所示,但它仍然存在。在 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];
最佳答案
您没有释放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 执行“分析”。 (它在“构建”菜单项下方。)我认为这应该解决忘记释放内部 messageString
的问题。在运行仪器之前使用“分析”。
关于苹果手机 : how to solve this memory leak?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7714394/