Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone内存泄漏_Iphone_Memory Leaks_Uiwebview_Overhead - Fatal编程技术网

iPhone内存泄漏

iPhone内存泄漏,iphone,memory-leaks,uiwebview,overhead,Iphone,Memory Leaks,Uiwebview,Overhead,我一直被iphone应用程序的内存泄漏问题困扰了很长一段时间。我觉得我的数据读错了。似乎每当我在内存中分配内存时,就会有太多的开销导致泄漏,以至于当我释放数据时,我的内存使用率几乎没有下降,或者根本没有下降。其中一个浪费了2天的是我的flipside view控制器上的UIWebview加载了一个url,我的应用程序的内存使用量从3MB跃升到7MB。我用dealoc方法发布了webview,但是巨大的内存块仍然存在。有人有什么建议吗 - (void)viewDidLoad { self.view

我一直被iphone应用程序的内存泄漏问题困扰了很长一段时间。我觉得我的数据读错了。似乎每当我在内存中分配内存时,就会有太多的开销导致泄漏,以至于当我释放数据时,我的内存使用率几乎没有下降,或者根本没有下降。其中一个浪费了2天的是我的flipside view控制器上的UIWebview加载了一个url,我的应用程序的内存使用量从3MB跃升到7MB。我用dealoc方法发布了webview,但是巨大的内存块仍然存在。有人有什么建议吗

- (void)viewDidLoad {
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];

nav_bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width+20,45)];
[self.view addSubview:nav_bar];
[UINavigationBar release];

rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done)];
item = [[UINavigationItem alloc] initWithTitle:@"Flipside View"];
item.rightBarButtonItem = rightButton;
item.hidesBackButton = YES;
[nav_bar pushNavigationItem:item animated:NO];
[rightButton release];
[item release];

NSAutoreleasePool *initPool = [[NSAutoreleasePool alloc] init];

web_view = [[UIWebView alloc]initWithFrame:CGRectMake(0,45,self.view.frame.size.width,self.view.frame.size.height - 45)];

web_view.autoresizesSubviews = YES;
web_view.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);

NSString *urlAddress = @"http://www.tutorialpark.com/wpcontent/uploads/3/HeartBlending.jpg";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

[web_view loadRequest:requestObj];
[self.view addSubview:web_view];
[web_view release];
[initPool release];

[super viewDidLoad];
}

- (void)dealloc {
   [nav_bar removeFromSuperview];
   [web_view removeFromSuperview];
   [rightButton release];
   [super dealloc];
}

我为缩进道歉,我现在非常生气,不想处理它。

[UINavigationBar release];-这是干什么的?是否意味着[导航栏释放]?-如果是这样的话,应该稍后再做,因为代码稍微向下一点,您可以再次访问导航栏。但它似乎是一个成员变量?所以它应该在dealloc中发布

rightButton被释放两次-一次在viewDidLoad中,一次在dealloc中


您能解释一下自动发布池的用途吗?

您似乎对引用计数的工作原理感到困惑

见以下评论:

- (void)viewDidLoad {
  self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];

  UINavigationBar* nav_bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width+20,45)];
  // nav_bar retaincount 1
  [self.view addSubview:nav_bar];
  // nav_bar retaincount 2
  [nav_bar release];
  // nav_bar retaincount 1 - now controlled by self.view

  UIBarButtonItem* rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done)];
  // rightButton retaincount 1

  UINavigationItem* item = [[UINavigationItem alloc] initWithTitle:@"Flipside View"];
  // item retaincount 1

  item.rightBarButtonItem = rightButton;
  // rightButton retaincount 2

  [rightButton release];
  // rightButton retaincount 1 - now controlled by item

  item.hidesBackButton = YES;
  [nav_bar pushNavigationItem:item animated:NO];
  // item retaincount 2

  [item release];
  // item retaincount 1 - now controlled by nav_bar

  UIWebView* web_view = [[UIWebView alloc]initWithFrame:CGRectMake(0,45,self.view.frame.size.width,self.view.frame.size.height - 45)];
  // web_view retaincount 1

  web_view.autoresizesSubviews = YES;
  web_view.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);

  NSString *urlAddress = @"http://www.tutorialpark.com/wpcontent/uploads/3/HeartBlending.jpg";

  NSURL *url = [NSURL URLWithString:urlAddress];
  // url is autoreleased, you can ignore..

  NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
  // requestObj is autoreleased, you can ignore..

  [web_view loadRequest:requestObj];

  [self.view addSubview:web_view];
  // web_view retaincount 2

  [web_view release];
  // web_view retaincount 1 - now controlled by self.view

  [super viewDidLoad];
}

- (void)dealloc {
   // don't need to do anything because all the memory is controlled by self.view, will be released when the internal variable self.view is released.
   [super dealloc];
}

顺便说一句,我在模拟器和真实设备中都看到了这些漏洞。自动释放池是从一段旧代码中遗留下来的。我一定是在发表文章时浏览过它的,这些文章明确地为我澄清了一些问题,但在我关闭filpside视图后,webview加载的数据仍然存在。这很好,因为它不必重新加载url,但我希望它不再缓存在内存中。有没有办法释放加载到webview中的数据?我认为这可以通过webview释放来完成,但根据我手边的分配图,情况并非如此;您可以添加和减去保留计数。在上面的例子中,注释很可能是不正确的——保留计数可能是2,可能是5,可能是492,重要的是这并不重要。你应该只担心你的原因。的确,计数可能是任何东西,但它更应该是一个粗略的心理指南,以了解计数将在哪里改变,以及大致的所有权转移发生在哪里。快速搜索发现UIWevView实际上可能是内存泄漏: