反向地理编码器自动释放问题
我正在学习使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否像 Apple 示例应用程序 CurrentAddress 中那样为
geoCoder
定义了保留属性?在他们的示例应用程序中,地理编码器是使用属性访问器设置的,否则当您退出该方法时,自动释放将释放 ivar。
将该行更改为:
另外请务必在
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:
Also be sure to release it in
dealloc
: