打电话给“[self.view release];”停止内存泄漏?

时间:2011-11-09 10:30:24

标签: ios memory memory-leaks viewcontroller

我对Objective-C很新,虽然我认为我理解引用计数的基础知识,但我并不理解我所看到的一些行为。

在Instruments中,我注意到在2个ViewControllers之间移动时,我的内存泄漏非常严重。据我所知,每次调用alloc都会调用release,但我确定我会犯一些菜鸟错误。

但是,我之前发现了这个问题:

 [self.view release];

阻止泄漏。但是,它确实为“未分配指针”的输出日志添加了错误。显然这是在修复我做错了的事情,但我不确定为什么要在那里调用呢?

[super dealloc];

应该(?)做什么。

我确信它充满了问题,但是如果有人能提供帮助我真的很感激 - 用这几天撕掉我的头发:/

以下是WatchViewController的代码:

#import "WatchViewController.h"

@implementation WatchViewController

@synthesize backButton = _backButton, scrollView = _scrollView, image;

...

-(void) backButtonPressed
{
    [self.navigationController popViewControllerAnimated:YES];
}

...

- (void)loadView
{
UIView *locView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = locView;

[locView release];
}

-(void) dealloc
{
self.image = nil;
self.backButton = nil;
self.scrollView = nil;

[super dealloc];
}

- (void)viewDidLoad
{
[super viewDidLoad];

NSString *str = [[NSBundle mainBundle] pathForResource:@"back_button" ofType:@"png"];
UIButton *locButton = [[UIButton alloc] init];
UIImage *locImage = [[UIImage alloc] initWithContentsOfFile:str];
[locButton setImage:locImage forState:UIControlStateNormal];
[locButton setFrame:CGRectMake(0, 0, locImage.size.width, locImage.size.height)];
[locButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];

self.backButton = locButton;
[self.view addSubview:_backButton];

[locImage release];
[locButton release];

[self.view release];
}

0 个答案:

没有答案