简体   繁体   中英

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.

在此处输入图片说明

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 . 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.

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块内的字符串。

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 release or 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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