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开发者_StackOverflow... 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.
Make sure to release the string inside the if
block.
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.
精彩评论