你好,我遇到了 UIWebView 内存泄漏的问题,基本上我的 WebView 显示页面的链接位于另一个 Controller 的 UITableView 中。我使用带有导航器的 WebView 推送 Controller ,并将带有保留属性的链接传递给
我已经尝试了互联网上的所有方法,例如:
[[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,当我点击导航器上的后退按钮时,我得到了这个泄漏。
这是来 self 的 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];
}
这是屏幕链接:http://postimage.org/image/368r0g0xw/
任何帮助将不胜感激, 谢谢
最佳答案
-(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];
}
现在我回到 TableView 后释放它,释放它后它仍然留下大约 3 MB,但是现在当我再次创建 View 并再次释放它时,这 3 MB 没有变化。奇怪...
Item Detail *webViewController=[[ItemDetail alloc] initWithNibName:@"ItemDetail" bundle:[NSBundle mainBundle] ];
[self.navigationController pushViewController:webViewController animated:YES];
[webViewController release];
关于IOS UIWebView 泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6308826/