Xcode指示潜在的对象泄漏



首先我是一个n00b。经过长时间的尝试和研究,我决定寻求一些外部帮助。我的项目:我为孩子们写了一本书。现在我正在分析我的代码,并试图摆脱一些泄漏(水平1 + 2崩溃一段时间后)。这是我的代码

- (void)loadView {
    _oben = YES;
    _unten = NO;
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
    UIImage *cover = [UIImage imageNamed:@"Umschlag.png"]; //Here it says "Potential leak..
    //..allocated on line 141 (thats at self.view = [[UIView alloc] initWithFrame:...
    image = [[UIImageView alloc] initWithImage:cover];
    image.frame = CGRectMake(0, 0, 768, 1024);
    [self.view addSubview:image];
    [image release];
    UITextView *text1 = [[UITextView alloc] initWithFrame:CGRectMake(184, 700, 400, 100)];
    text1.backgroundColor = [UIColor clearColor];
    text1.textAlignment = UITextAlignmentCenter;
    text1.text = NSLocalizedString(@"CoverTextKey1", nil);
    [self.view addSubview:text1];
    [text1 release];
    [self addButtonNext];
    [self addSwipeDown];
    [self addSwipeUp];
}

任何想法?如果有人能帮我,那就太好了!提前感谢Planky

在第141行分配潜在泄漏:

自我。view = [[UIView alloc] initWithFrame:[[UIView mainScreen] applicationFrame]];

那一行是过度保留对象,因为alloc-init返回一个保留的对象(+1),并且属性设置器也保留了该对象(+2)。

你可以使用一个临时变量…

UIView *temp = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = temp;
[temp release];

…或者自动释放来修复这个问题:

self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]; 

使用+ (UIImage *)imageWithContentsOfFile:(NSString *)path- (id)initWithContentsOfFile:(NSString *)path代替。它们不缓存图像,imageNamed:缓存。

最新更新