0

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

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

代码如下所示: .h

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

他们

@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因此,我在of中打印出内存地址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 的所有可能的地方。但是,我找不到任何东西。

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

4

1 回答 1

2

我怀疑这不是涉及addAnnotation变量的整个代码。最有可能[mapView removeAnnotation:addAnnotation];的,释放addAnnotation的,已经使引用计数下降到零。你的代码中有这样的东西吗?

 [mapView addAnnotation:addAnnotation];
 [addAnnotation release];

如果是这样,那么您已经将 addAnnotation 的完整所有权转移到了 mapView 中,您不需要再释放它showAddress,这意味着removeAnnotation:已经足够了。

于 2010-10-10T09:54:44.307 回答