【发布时间】: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