反向编码器自动释放问题

时间:2011-09-02 21:09:39

标签: ios autorelease mkreversegeocoder

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

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

以上工作正常,但是...在尝试通过考虑管理内存和资源来改进我的应用程序时,我正在尝试添加自动释放geoCoder分配:

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

苹果在他们的文档中使用了上述模式,因此它似乎是正确的做法。但是,当我添加autorelease时,didFindPlacemark方法永远不会被调用。就像autorelease立即释放geoCoder对象一样。

geoCoder对象被声明为ivar,因此它应该可以工作。使用这种模式的苹果示例是有效的,所以问题必须与我的实现有关,但我无法解决我出错的地方。

我很感激任何人输入最新动态以及如何实现这一目标。

最好的问候

1 个答案:

答案 0 :(得分:1)

您是否在Apple样本应用CurrentAddress中为geoCoder定义了保留属性?

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

将该行更改为:

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

另请务必在dealloc

中发布
- (void)dealloc
{
    [geoCoder release];
    [super dealloc];
}
相关问题