2011-04-29 168 views
1

我在看這個代碼從MapViewController.m文件直解禁的CurrentAddress sample可在蘋果公司的網站:MKReverseGeocoder自動釋放/釋放問題

- (void)dealloc 
{ 
    [reverseGeocoder release]; 
    [mapView release]; 
    [getAddressButton release]; 

    [super dealloc]; 
} 

- (IBAction)reverseGeocodeCurrentLocation 
{ 
    self.reverseGeocoder = 
     [[[MKReverseGeocoder alloc] initWithCoordinate:mapView.userLocation.location.coordinate] autorelease]; 
    reverseGeocoder.delegate = self; 
    [reverseGeocoder start]; 
} 

我想知道的功能是什麼分配對象時的自動釋放。 (reverseGeocoder是在設置了retain屬性的MapViewController類中的一個ivar。)我在我的應用程序中有類似於此的代碼,並且它似乎以任何方式工作。

回答

2

設置你的reverseGeocoder財產遞增保留計數(+1),但因爲你是用alloc + init(+1)創建對象,你需要autorelease(-1),這樣你不就結了2保留計數。

它確實可以工作,唯一的區別是當你做不是autorelease,你會泄漏。

的reverseGeocoder是伊娃

可以肯定的是,但是請注意,當你使用self.reverseGeocoder形式,你不是直接訪問伊娃 - 相反,你調用相關setReverseGeocoder:函數,即由您自己編寫或由編譯器編寫的@synthesized函數。

參見:http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/MemoryMgmt/MemoryMgmt.html

和: What equivalent code is synthesized for a declared property?