2010-10-10 43 views
0

当我试图释放一个实例变量并重新分配一个新值时,会发生问题。实例变量的dealloc问题

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

的代码如下所示: 的.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总是走来,如果过程中,通过它运行。

因此我在AddressAnnotationManagerdealloc印出的内存地址:

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

我打开僵尸,控制台给了我这样的事情:

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

2010-10-10 17:02:54.396 [1908:207] - [AD dressAnnotationManager发布]:发送到释放实例的消息0x46c7360 *

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

我已经检查了所有可能发布addAnnotation的地方。但是,我找不到任何。

有没有人碰巧找到问题所在?

回答

2

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

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

如果是这样,那么你已经转移addAnnotation到的MapView完整的所有权,你不需要释放它在showAddress更多,这意味着removeAnnotation:就够了。