2011-05-24 46 views
0

嘿,所有。我很頭疼,想要解決這個問題。我有一個NSNumber類型的保留屬性。當我使用這個屬性時,我用自動發佈的NSNumber實例化它。當dealloc被調用時,我會得到不好的訪問,告訴我我釋放了已經釋放的東西。這裏有一些代碼。發佈已被自動發佈的保留財產?

@interface RadarAnnotation : NSObject <MKAnnotation> {  
} 
@property (retain, nonatomic) NSNumber *latitude; 
@end 

@implementation RadarAnnotation 

@synthesize latitude; 

- (CLLocationCoordinate2D)coordinate 
{ 
coordinate.latitude = [self.latitude doubleValue]; 
return coordinate; 
} 

-(void) dealloc { 
[super dealloc]; 
[latitude release];//error is here when mapViewController is popped off stack. 
} 

這裏是我實例化屬性在我的MapViewController:

poi.latitude = [NSNumber numberWithDouble:map.centerCoordinate.latitude]; 

我在做什麼錯?非常感謝。朱爾斯。

回答

1

你應該在dealloc方法的末尾調用[super dealloc]。

+0

謝謝你的工作。爲什麼是這樣? – Jules 2011-05-24 22:07:56

+1

因爲NSObject的-dealloc實現處理實際上釋放事物。如果你重寫它並且不調用超類的實現,那麼任何事情都不會被釋放。因此,所有的dealloc方法最後都必須調用[super dealloc]。 – 2011-05-25 04:14:59