2010-10-10 63 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:就夠了。