Apple CurrentAddress 示例中的 MKReverseGeocoder 自动释放/释放问题

发布于 2024-11-04 00:56:21 字数 537 浏览 0 评论 0原文

我正在查看直接从

- (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抚你发端 2024-11-11 00:56:21

设置 reverseGeocoder 属性会增加保留计数 (+1),但由于您是使用 alloc+init 创建对象 (+1) ,您需要autorelease (-1),这样您就不会得到 2 的保留计数。

无论哪种方式它都可以工作,唯一的区别是,当您自动释放时,就会发生泄漏。

reverseGeocoder 是一个 ivar

确实如此,但请注意,当您使用 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 with alloc+init (+1), you need to autorelease (-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.

The reverseGeocoder is an ivar

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.

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文