Apple的CurrentAddress示例中的MKReverseGeocoder自动释放/释放问题 [英] MKReverseGeocoder autorelease/release question in Apple's CurrentAddress sample
问题描述
我正在查看直接从 CurrentAddress示例可在Apple网站上找到:
I am looking at this code lifted straight from the MapViewController.m file in the CurrentAddress sample available on Apple's web site:
- (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是MapViewController类中的一个带有保留属性的ivar.)在我的应用程序中,我有与此类似的代码,并且无论哪种方式都可以工作.
I am wondering what the function is of the autorelease when allocating the object. (The reverseGeocoder is an ivar in the MapViewController class set up with the retain property.) I have code similar to this in my application, and it seems to work either way.
推荐答案
设置您的reverseGeocoder
属性会增加保留计数(+1),但是由于您要使用alloc
+ init
(+ 1),您需要autorelease
(-1),以免最后保留2个计数.
Setting your reverseGeocoder
property increments the retain count (+1), but since you're creating the object with alloc
+init
(+1), you need to autorelease
(-1) so that you do not end up with a 2 retain count.
无论哪种方式都可以工作,唯一的区别是,当您执行不 autorelease
时,会泄漏.
It does work either way, the only difference is that when you do not autorelease
, you leak.
reverseGeocoder是一个ivar
The reverseGeocoder is an ivar
确实可以,但是请注意,当您使用self.reverseGeocoder
表单时,并不是直接访问ivar,而是调用相关的setReverseGeocoder:
函数,该函数由您自己编写或@由编译器合成.
It sure is, but note that when you're using the self.reverseGeocoder
form, you're not accessing the ivar directly - instead, you're calling the relevant setReverseGeocoder:
function, that is either written by yourself or @synthesized by the compiler.
请参阅: http://developer. apple.com/library/mac/#documentation/cocoa/conceptual/MemoryMgmt/MemoryMgmt.html
并且: 为声明的属性合成了什么等效代码?
这篇关于Apple的CurrentAddress示例中的MKReverseGeocoder自动释放/释放问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!