简体   繁体   English

iPhone:如何解决此内存泄漏?

[英]iphone : how to solve this memory leak?

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. 如果语句几乎是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];

You're not releasing messageString . 没有释放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. 从XCode执行“分析”。 (It's below the "build" menu item.) I think that should capture this problem of forgetting to release the inner messageString . (它在“ build”菜单项的下面。)我认为这应该可以解决忘记释放内部messageString Use "Analyze" before running Instruments. 运行仪器之前,请使用“分析”。

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. 还要注意,您可能使用将messageString作为参数传递的方法可能也会保留它。

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

The basic rule of thumb is that for every alloc , new , retain , or copy , you need a release or autorelease . 基本的经验法则是,对于每个allocnewretaincopy ,您都需要releaseautorelease release It seems that you are missing a release or autorelease somewhere. 似乎您在某处缺少releaseautorelease release

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. 顺便说一句,您可以(并且应该)使用Xcode的“ Build and Analyze”(构建和分析)来帮助发现内存泄漏,甚至可以部署到测试设备上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM