2011-06-10 91 views
5

你好,我有一個UIWebView泄漏內存的問題基本上我有我的WebView顯示頁面的鏈接是從另一個控制器的UITableView.I推動與導航器的WebView的控制器,並通過與鏈接保留財產。IOS的​​UIWebView泄漏

我已經試過各種方法在互聯網上,如:

[[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]; 

這是我的代碼:

-(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]; 
} 

現在我只在模擬器4.2和4.3上測試過,我使用xcode 4,當我點擊導航器上的後退按鈕時,我得到了這個漏洞。

這裏是從我的tableview控制器

- (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]; 
} 

這裏的代碼到屏幕的鏈接:http://postimage.org/image/368r0g0xw/

任何幫助,將不勝感激, 感謝

+0

你的一行代碼出現在任何方法之外,你可以發佈你的實際代碼嗎?這不會編譯。 「myRequest」在哪裏被分配/分配?關於'link'呢?你有沒有爲UIWebView設置一個委託? – Jim 2011-06-10 16:05:40

+0

對不起,我現在會發布代碼 – 2011-06-29 08:43:54

回答

1
-(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]; 
} 

現在我在我回到表格視圖後釋放它,它釋放後仍然留下約3 MB,但現在當我再次創建視圖並釋放它時在那3mb中沒有變化。奇怪...

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


    [self.navigationController pushViewController:webViewController animated:YES]; 
    [webViewController release];