我得到了内存泄漏,我无法通过泄漏,构建/分析或整体检查如何修复。我有一个非常强烈的概念,那是因为我的UIWebview加载了javascript的loadRequest命令,但我无法弄清楚是什么问题。
这是我的设置:
我有一个tabbarcontroller,我用4个视图控制器以编程方式创建:表视图,mapview,另一个表视图和webview。
当我最终从webview执行loadRequest()时,最初一切正常。然而,当我开始在标签之间切换并查看不同的3D模型时(即转到地图视图,单击注释以显示模型x,在webview中观看它,然后切换到列表并单击一行以显示模型y(或者x也是)并且在webview中观察它的负载)我开始在ipad上接收内存泄漏,而不是模拟器。这些内存泄漏几乎总是如下:General-Block 56,1024,8,244& 24并没有负责任的框架,库或堆栈跟踪跟进。
在webview中如果我注释掉loadrequest行或者使用请求对象= nil执行loadrequest,我没有内存泄漏,但是当我切换到使用指向本地文件的url对象时(我甚至尝试指向它到google.com,它有javascript)它会在内存泄漏中爆发。
我试图做以下每一件事:
在我的webViewController的viewWillDisappear中尝试完全清理webview以便将来重用,但它似乎没有提供太多帮助。我还尝试在执行loadRequest之前执行此操作,但我得到了相同的内存泄漏。
下面是单击mapView上的其中一个注释的示例代码,它触发webViewController中的loadModel函数:
- (void) mapCallOutPressed: (UIView *) sender {
NSInteger selectedIndex = sender.tag;
MyLocation *selectedObject = [_mapView.annotations objectAtIndex:selectedIndex];
MyModel *model = selectedObject.model;
NSLog(@"Model selected from mapview = %@", model.description);
// Get reference to the webview from the tabBarController (viewController's index=3)
WebViewController *webViewController = [self.tabBarController.viewControllers objectAtIndex:3];
webViewController.model = model;
[webViewController loadModel];
// Switch tabs to the webView
self.tabBarController.selectedIndex = 3;
[self.tabBarController.selectedViewController viewDidAppear:YES];
下面是我的webViewController.h&的.m:
WebViewController.h:
WebViewController : UIViewController <UIWebViewDelegate> {
UIWebView *webView;
NSString *templateRunFilePath;
MyModel *model;
NSString *templateRunTitle;
@property (nonatomic, retain) UIWebView *webView;
@property (assign) MyModel *model;
@property (nonatomic, copy) NSString *templateRunFilePath;
@property (nonatomic, copy) NSString *templateRunTitle;
-(void) loadModel;
WebViewController.m:
- (void) viewDidLoad {
[super viewDidLoad];
NSLog(@"in webview did load");
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
self.webView.delegate = self;
self.webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
self.webView.scalesPageToFit = YES;
[self.view addSubview:self.webView];
}
- (void) viewWillAppear:(BOOL)animated {
if (self.model) {
self.tabBarController.navigationItem.title = [NSString stringWithFormat: @"%@ - %@", self.tabBarController.navigationItem.title, self.model.description];
}
else {
UIAlertView *msg = [[UIAlertView alloc] initWithTitle:@"No Model Selected"
message:@"Please select a model from the map or list tab to view."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[msg show];
[msg release];
}
}
- (void) loadModel {
NSString *localURLPath = [NSString stringWithFormat:@"%@/%@/%@", self.templateRunFilePath, self.model.folderName, self.model.modelFileName];
NSURL *url = [NSURL fileURLWithPath:localURLPath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL: url];
[self.webView loadRequest:requestObj];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
}
-(void)viewWillDisappear:(BOOL)animated {
NSLog(@"webview view will disappear");
[super viewWillDisappear:animated];
//[self.webView loadRequest:nil];
//[self.webView loadHTMLString:@"" baseURL:nil];
self.webView.delegate = nil;
}
- (void)dealloc {
self.webView.delegate = nil;
[self.webView release];
[super dealloc];
}
如果您有任何建议或更正,我将非常感激。我有&lt;一个星期来解决这个问题,如果你能给我任何见解,我将非常感谢你。
谢谢!
-Mike
答案 0 :(得分:0)
- (void) loadModel {
[self.webView loadHTMLString:@"" baseURL:nil]; // here you are loading... or you have tried with commenting it also?
NSString *localURLPath = [NSString stringWithFormat:@"%@/%@/%@", self.templateRunFilePath, self.model.folderName, self.model.modelFileName];
NSURL *url = [NSURL fileURLWithPath:localURLPath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL: url];
[self.webView loadRequest:requestObj]; // again here you are also loading?
}