Apple CurrentAddress 示例中的 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 是 MapViewController 类中使用保留属性设置的 ivar。)我的应用程序中有与此类似的代码,并且它似乎可以以任何方式工作。
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];
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置
reverseGeocoder
属性会增加保留计数 (+1),但由于您是使用alloc
+init
创建对象 (+1) ,您需要autorelease
(-1),这样您就不会得到 2 的保留计数。无论哪种方式它都可以工作,唯一的区别是,当您不
自动释放
时,就会发生泄漏。确实如此,但请注意,当您使用
self.reverseGeocoder
表单时,您不会直接访问 ivar - 相反,您正在调用相关的setReverseGeocoder:
函数,可以由您自己编写,也可以由编译器@synthesized。请参阅:http://developer.apple.com /library/mac/#documentation/cocoa/conceptual/MemoryMgmt/MemoryMgmt.html
并且:
为声明的属性合成了哪些等效代码?
Setting your
reverseGeocoder
property increments the retain count (+1), but since you're creating the object withalloc
+init
(+1), you need toautorelease
(-1) so that you do not end up with a 2 retain count.It does work either way, the only difference is that when you do not
autorelease
, you leak.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 relevantsetReverseGeocoder:
function, that is either written by yourself or @synthesized by the compiler.See: http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/MemoryMgmt/MemoryMgmt.html
And:
What equivalent code is synthesized for a declared property?