0

以下是我的代码:

    UIImage *takePhotoImg = [UIImage imageNamed:@"add_pic.png"];
    self.takePhoto = [[UIButton alloc] initWithFrame:CGRectMake(120, 100, takePhotoImg.size.width, takePhotoImg.size.height)];
    [_takePhoto setImage:takePhotoImg forState:UIControlStateNormal];
    [_takePhoto addTarget:self action:@selector(takePhotoBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_takePhoto];

当我使用分析时,它显示以下行:

[_takePhoto setImage:takePhotoImg forState:UIControlStateNormal];

分配的对象的潜在泄漏。我需要添加版本,还是忽略?
先感谢您

更新:我确实在我的 dealloc 中释放了按钮 _takePhoto:

-(void)dealloc
{
    [_takePhoto release];
    [super dealloc];
}

我的财产 :

@property(nonatomic,retain)UIButton *takePhoto;
4

2 回答 2

0

如果您不使用 ARC,那么是的,您需要释放它。当您将其添加到self.view的子视图时,self.view将保留它,因此按钮不会消失。

如果您不需要访问按钮并且不需要保留引用,则可以在addSubview:调用后立即释放它。

但是,它看起来像是takePhoto您班级的一个属性。如果是这种情况,并且您想保留对按钮的引用以供以后使用,只需将[_takePhoto release]调用添加到您的类dealloc方法即可。这应该会抑制代码分析警告。

于 2012-04-25T02:14:02.367 回答
0

更改代码:

    self.takePhoto = [[UIButton alloc] initWithFrame:CGRectMake(120, 100,      takePhotoImg.size.width, takePhotoImg.size.height)];

    self.takePhoto = [[[UIButton alloc] initWithFrame:CGRectMake(120, 100, takePhotoImg.size.width, takePhotoImg.size.height)] autorelease];

delloc 方法中的 [_takePhoto release] 用于 setter 方法中的 retain。每次调用 self.takePhoto = aNewTakePhote,aNewTakePhote 都会被保留一次。

于 2012-04-25T03:40:46.817 回答