iPhone可笑的内存泄漏
我已经被我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎对引用计数的工作原理感到困惑。
请参阅下面的评论:
You seems to be confused about how the reference counting works.
See comments below:
[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?