iPhone可笑的内存泄漏

发布于 2024-12-01 10:31:51 字数 1677 浏览 2 评论 0原文

我已经被我的 iPhone 应用程序的内存泄漏问题困扰了很长时间了。我觉得我的数据读错了。似乎每当我分配内存时,都会有太多的开销导致泄漏,当我释放数据时,我的内存使用量要么几乎没有下降,要么根本没有下降。其中浪费了 2 天的时间是我的翻转视图控制器上的 UIWebview 加载了一个 url,并且我的应用程序的内存使用量从 3 mb 跳到了 7。我在 dealloc 方法中释放了 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];
}

我对缩进表示歉意,我现在很生气,不想处理它。

I have been stuck on the problem of memory leaks in my iphone app for a good time now. I feel like I have to be reading my data wrong. it seems like whenever I allocate memory in there is so much overhead that is causing leaks that when I release the data my memory usage either barely drops or does not drop at all. One have wasted 2 days on is my UIWebview on my flipside view controller loads a url and the memory usage of my app jumps from 3 mb to 7. I release the webview in my dealloc method but the huge block of memory is still living. Does anybody have any suggestions.

- (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];
}

I apologize about the indentation I am very pissed off right now and don't want to deal with it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青朷 2024-12-08 10:31:51

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

请参阅下面的评论:

- (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];
}

You seems to be confused about how the reference counting works.

See comments below:

- (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];
}
饮惑 2024-12-08 10:31:51

[UINavigationBar发布]; - 这是在做什么?它是否意味着 [nav_bar release] ? - 如果是这样,应该稍后在您再次访问 nav_bar 的代码稍下方完成。但那好像是一个成员变量?所以应该在dealloc中释放。

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

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

[UINavigationBar release]; - What is this doing? Is it meant to be [nav_bar release] ? - If so it should be done later as slightly further down the code you access nav_bar again. But then it seems to be a member variable? So it should be released in dealloc.

rightButton is being released twice - Once in viewDidLoad and once in dealloc.

Can you please explain what the auto release pool is for?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文