实例变量的dealloc问题

时间:2010-10-10 09:47:55

标签: iphone objective-c release instance-variables

当我尝试释放我的一个实例变量并将其重新分配给新值时,就会出现问题。

我想释放实例变量指向的地址,并为其重新赋值。

代码如下所示: .h

@interface MapPageController : UIViewController<MKMapViewDelegate> {
 AddressAnnotationManager *addAnnotation;
}
- (IBAction) showAddress;
@property (nonatomic, retain) AddressAnnotationManager *addAnnotation;

.m

@synthesize addAnnotation;
- (IBAction) showAddress {
        if(addAnnotation != nil) {
  [mapView removeAnnotation:addAnnotation];
  [addAnnotation release]; // this generates the problem
  addAnnotation = nil;
 }
 addAnnotation = [[AddressAnnotationManager alloc] initWithCoordinate:location];
 addAnnotation.pinType = userAddressInput;
 addAnnotation.mSubTitle = addressField.text;
}

但是,对于[addAnnotation release],如果进程通过它,则始终会出现EXC_BAD_ACCESS。

因此,我打印了dealloc AddressAnnotationManager中的内存地址:

- (void)dealloc {
 NSLog(@"delloc Instance: %p", self);
 [super dealloc];
}

我打开Zombie,控制台给了我这样的东西:

2010-10-10 17:02:35.648 [1908:207] delloc实例:0x46c7360

2010-10-10 17:02:54.396 [1908:207] - [AddressAnnotationManager release]:发送到解除分配的实例0x46c7360的消息 *

这意味着在问题发生之前代码会达到dealloc

我已经检查了可以释放addAnnotation的所有可能位置。但是,我找不到任何。

有没有人碰巧发现问题所在?

1 个答案:

答案 0 :(得分:2)

我怀疑这不是涉及addAnnotation变量的整个代码。最有可能发布[mapView removeAnnotation:addAnnotation];的{​​{1}}已经使引用计数降为零。你的代码在某个地方有这样的东西吗?

addAnnotation

如果是这样,那么您已将addAnnotation的完整所有权转移到mapView,您无需再在 [mapView addAnnotation:addAnnotation]; [addAnnotation release]; 中释放它,这意味着showAddress就足够了。

相关问题