【问题标题】:IOS UIWebView LeakIOS UIWebView 泄露
【发布时间】:2011-06-10 15:47:52
【问题描述】:

您好,我遇到了 UIWebView 泄漏内存的问题,基本上我的 WebView 显示页面的链接位于另一个控制器的 UITableView 中。我使用导航器推送带有 WebView 的控制器,并将带有保留属性的链接传递给它。

我已经尝试了互联网上的所有内容,例如:

[[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/

任何帮助将不胜感激, 谢谢

【问题讨论】:

  • 您的其中一行代码出现在任何方法之外,您可以发布您的实际代码吗?那不会编译。 myRequest 在哪里被分配/分配? link 呢?您是否为UIWebView 设置了代理?
  • 抱歉,我现在就发布代码

标签: ios memory uiwebview memory-leaks


【解决方案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];

【讨论】:

    猜你喜欢
    • 2015-12-17
    • 2011-05-02
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 2012-06-02
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多