user1296841
user1296841

Reputation: 1

Xcode memory leaks?

I'm analyzed my code which uses a WebViewController and I am getting memory leaks.

Could this simple code really be causing such issues?

- (IBAction) google: (id) sender {
    NSURL *url = [NSURL URLWithString:@"http://www.google.com/"];
    WebViewController *webViewController = [[WebViewController alloc] initWithURL:url andTitle:@"Google"];
    [self presentModalViewController:webViewController animated:YES];
    //[webViewController release];
} 

If I uncomment the release, then there is no leak, but after a few clicks through Google, it will crash the program. So I'm not sure if I should leave the leak in the program since at least then it works. Can anyone provide some insight? So after navigating thorugh pages in the webview and click the Done button it will go back to my main view for 1 second and crash.

Crash Output (Under WebThread) http://pastebin.com/A8ELm18R

Upvotes: 0

Views: 175

Answers (1)

rosslebeau
rosslebeau

Reputation: 1283

You need to release the web view at a later time, after you are done using it. Keep track of it with a property and then when you dismiss the modal you can release it.

You could also use autorelease like such:

- (IBAction) google: (id) sender {
    NSURL *url = [NSURL URLWithString:@"http://www.google.com/"];
    WebViewController *webViewController = [[[WebViewController alloc] initWithURL:url andTitle:@"Google"] autorelease];
    [self presentModalViewController:webViewController animated:YES];
} 

Upvotes: 1

Related Questions