经过多天将我的头撞在墙上并度过了一个不眠之夜,我希望能在这里找到一些帮助.我在这里经历了各种各样的帖子,但没有一个答案似乎为我提供了解决方案.
简而言之,我的问题是我的应用程序在UIWebView大量使用(> 10分钟)后崩溃(例如,逐个打开更大的新闻纸张网站 - 一个接一个).
为了提供更多细节:我正在编写一个iPhone应用程序,并从MainViewController我在navigationController上推送一个browserViewController.browserViewController加载一个包含UWebView的笔尖(我不以编程方式创建WebView).UIWebView使用Interface Builder连接.
当回到Main然后再次访问browserViewController时,我只重新创建了browserViewController(如果它是nil).(我想保留在UIWebView中加载的内容 - 只有在有内存警告时才会卸载此视图并释放所有使用的内存).
在MainViewController和browserViewController中我都在响应内存警告,但这似乎没有提供足够的缓解.
看看仪器,我注意到例如CFData(商店)不断增加.即使我模拟了内存警告(请参阅下面的代码)并在browserViewController上调用viewDidUnload,CFData仍然会被分配并且不会被释放.
所以我最大的问题是:
如何释放"浏览"创建的内存?
这有两种情况:
- 如何确保viewDidUnload正确释放分配了我的CFData(存储)的内存?
- 当用户继续在browserViewController中加载页面时如何释放内存?
.
谁管理CFData?
请参阅下面的简化示例代码:
// MainViewController.h
#import "myAppDelegate.h"
#import "BrowserViewController.h"
@interface MainViewController : UIViewController {
BrowserViewController *browViewController;
}
- (void) switchToBrowserViewController;
@end
Run Code Online (Sandbox Code Playgroud)
// MainViewController.m
#import "MainViewController.h"
@implementation MainViewController
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
[browViewController release];
browViewController = nil;
}
- (void)viewDidUnload {
// Release any retained subviews of the main …Run Code Online (Sandbox Code Playgroud)