Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS UIWebView Leak

Hello i am having problems with an UIWebView leaking memory basicaly i have my WebView display pages with the links being in an UITableView from another controller.I push the controller with the WebView with an navigator and pass the link with a retain property to it.

I have tried everything on the internet like:

[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];

//Clear cache of UIWebView
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];
sharedCache = nil;
[[NSURLCache sharedURLCache] removeAllCachedResponses];

this is my code:

-(void) viewWillAppear:(BOOL) animated
{
    NSMutableURLRequest *_req=[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:link] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:120];

    [_req setHTTPShouldHandleCookies:NO];
    [self setMyRequest:_req];
    [req release];
}

[webView loadRequest:myRequest];

-(void) viewWillDisappear:(BOOL) Animated
{
    [webView stopLoading];
    [webView loadHTMLString:@"<html></html>" baseURL:nil];
}

- (void)dealloc {
    [myRequest release];
    [webView stopLoading];
    [webView release];
    [link release];
    [super dealloc];
}

Now I only tested in on simulator 4.2 and 4.3, i use xcode 4, i get this leaks when i hit the back button on the navigator.

And here is the code from my tableview Controller

- (void)viewDidLoad {
    webViewController=[[ItemDetail alloc] initWithNibName:@"ItemDetail" bundle:[NSBundle mainBundle] ];

    [super viewDidLoad];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    webViewController.link=http://www.myLink.com;
    [self.navigationController pushViewController:webViewController animated:YES];

}

-(void) dealloc
{
    [webViewController release];
    ...
    ...
    [super dealloc];
}

Here is a link to a screen: http://postimage.org/image/368r0g0xw/

Any help would be appreciated, Thanks

like image 498
Andrei Erdei Avatar asked Nov 14 '22 21:11

Andrei Erdei


1 Answers

-(void) viewWillAppear:(BOOL) Animated
{
    //CONNECTION INIT
    [web setDelegate:self];

    NSURL *url=[[NSURL alloc] initWithString:link];

    NSURLRequest *_req=[[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:120];

    [self setReq:_req];

     [_req release];
     [url release];
    [web loadRequest:req];


}

-(void) viewWillDisappear:(BOOL) Animated
{
    [self setReq:nil];
    if ([web isLoading])
    [web stopLoading];
    [web setDelegate:nil];
}

- (void)dealloc {
    [req release];
    [map release];
    [web setDelegate:nil];
    [web release];
    [link release];
    [super dealloc];
}

Now i release it after i go back to the table view, it still leaves about 3 mb after i release it, but now when i create the view again and release it again those 3mb are unchanged. Weird...

Item Detail *webViewController=[[ItemDetail alloc] initWithNibName:@"ItemDetail" bundle:[NSBundle mainBundle] ];


    [self.navigationController pushViewController:webViewController animated:YES];
    [webViewController release];
like image 112
Andrei Erdei Avatar answered Dec 21 '22 22:12

Andrei Erdei