反向地理编码器自动释放问题

发布于 2024-12-02 12:02:05 字数 655 浏览 0 评论 0原文

我正在学习使用 mkreversegeocoder 类,并使用以下三行代码并实现 didFindPlacemark 方法使其正常工作。

geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:[u coordinate]];
[geoCoder setDelegate:self];
[geoCoder start];

上面的方法工作正常,但是......在尝试通过考虑管理内存和资源来改进我的应用程序时,我试图添加一个自动释放的 geoCoder 分配,如下所示:

geoCoder = [[[MKReverseGeocoder alloc] initWithCoordinate:[u coordinate]] autorelease];

上述模式由苹果在其文档中使用,所以看起来像正确的做法。但是,当我添加自动释放时,didFindPlacemark 方法永远不会被调用。就好像自动释放立即释放了 geoCoder 对象。

geoCoder 对象被声明为 ivar,因此它应该可以工作。使用这种模式的苹果示例有效,因此问题一定出在我的实现上,但我无法弄清楚我哪里出了问题。

我非常感谢任何人对正在发生的事情以及我如何才能做到这一点提供意见。

此致

I'm learning to use the mkreversegeocoder classes and have got it working using the following three lines of code and implementing the didFindPlacemark method.

geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:[u coordinate]];
[geoCoder setDelegate:self];
[geoCoder start];

The above works fine, however... in trying to improve my app by thinking about managing memory and resources, i am trying to add an autorelease the geoCoder allocation as such:

geoCoder = [[[MKReverseGeocoder alloc] initWithCoordinate:[u coordinate]] autorelease];

The above pattern is used by apple in their documentation so it seems like the right thing to do. However, when I add the autorelease, the didFindPlacemark method never gets called. It's as if the autorelease releases the geoCoder object immediately.

The geoCoder object is declared as an ivar, so it should work. the apple example using this pattern works so the problem must be with my implementation, but i cannot work out where i am going wrong.

i would appreciate anyone input as to whats happening and how i can get this going.

best regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

关于从前 2024-12-09 12:02:05

您是否像 Apple 示例应用程序 CurrentAddress 中那样为 geoCoder 定义了保留属性?

在他们的示例应用程序中,地理编码器是使用属性访问器设置的,否则当您退出该方法时,自动释放将释放 ivar。

将该行更改为:

self.geoCoder = [[[MKReverseGeocoder alloc] initWith...] autorelease];

另外请务必在 dealloc 中释放它:

- (void)dealloc
{
    [geoCoder release];
    [super dealloc];
}

Have you defined a retain property for geoCoder like in the Apple sample app CurrentAddress?

In their sample app, the geocoder is set using the property accessor otherwise the autorelease will release the ivar when you exit the method.

Change that line to:

self.geoCoder = [[[MKReverseGeocoder alloc] initWith...] autorelease];

Also be sure to release it in dealloc:

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