Search code examples
objective-ciosmemory-managementmemory-leaksinstruments

Instruments and memory leaks


I'm using the leaks tool in XCode's instruments to find memory leaks (go figure). I have a few leaks each time I run my app at different points in the app. I look at the extended details, and I'm never pointed to any of the code I wrote, only code built into the foundation of xcode. Two examples of this are:

http://imageshack.us/photo/my-images/192/screenshot20110728at102.png/

http://imageshack.us/photo/my-images/853/screenshot20110728at102.png/

As you can see, some of the problems come from the Message UI library. The only place I use that is here:

-(void)displayComposerSheet
{
    MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;

[mail setSubject:@"Suggestions"];

[mail setToRecipients:[NSArray arrayWithObjects:@"[email protected]", nil]];
[self presentModalViewController:mail animated:YES];
[mail release];
}

-(void)launchMailAppOnDevice
{
NSString *recepient = [NSString stringWithFormat:@"mailto:[email protected]"];
recepient = [recepient stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:recepient]];
}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissModalViewControllerAnimated:YES];
}

How can I fix this? Thanks!


Solution

  • Finding out where a leak comes from is not always an easy task, helping remotely is even more complex.

    In your case, what could help is the little arrow that is shown next to the memory address of the leak. If you click on it, you should get shown an information pane with the full stack trace at that moment. Inspect the list of methods presented there and look for some method of yours, then click on it to inspect the code.

    There is not much else you can do in Instruments that get an idea about where the leaked object was created. You should then figure out where you could have missed releasing it.

    As to Apple SDK, there are a few memory leaks that are reported and that you can find out there on the web, but they are really seldom.

    If Instruments does not help you, one important consideration for you is: what was your app doing when Instruments reported the leak? Retrieving data from the web, displaying a new view, whatever... this could help in delimiting the field of your further investigation. If the leaks shows up multiple times, this could also be a great value to identify what part of your program could be producing it.

    If you get a clue about that, then go check the code that accomplishes that. One technique (I know that it might sound strange, but try it) is removing/commenting out chunks of code and then checking to see whether the leak is still there.

    "Removing/commenting out chunks of code" may mean many things, from not making a web request, to avoid using a class of a type and replacing it with another. This is not always a simple process, because you impair the app functionality, so you have to use your knowledge about how remove functionality and leave your app "testable". If you are lucky, this could help you further delimiting the code that is causing the leak.

    Keep in mind that also the static analysis tool could help, and that leaks is not perfect, sometimes it is wrong. There is another technique to discover leaks that is not based on Leaks, but on Object Allocation. This is very powerful in my experience and I strongly encourage you to try that also, although I suspect that it will not help in this case. It is called heapshot analysis.