【问题标题】:potential leak of an object allocated and stored into 'annot'分配并存储到“annot”中的对象的潜在泄漏
【发布时间】:2012-03-06 09:43:16
【问题描述】:

我有很多这样的注释(大约 2400 个),但这是我的问题。

我收到以下错误

在第 81 行分配并存储到的对象的潜在泄漏 'annot1' 在第 81 行分配并存储到“annot2”中的对象的潜在泄漏

在第 81 行分配并存储到的对象的潜在泄漏 'annot3'

等等。这是我的代码:

MKPointAnnotation *annot1 = [[MKPointAnnotation alloc] init]; 
annot1.title = @"A"; 
annot1.subtitle=@"A1"; 
annot1.coordinate = CLLocationCoordinate2DMake(21.978954, 120.752663); 
[mapView addAnnotation:annot1]; 
MKPointAnnotation *annot2 = [[MKPointAnnotation alloc] init]; 
annot2.title = @"B"; 
annot2.subtitle=@"B2"; 
annot2.coordinate = CLLocationCoordinate2DMake(21.988607, 120.748703); 
[mapView addAnnotation:annot2]; 

MKPointAnnotation *annot4 = [[MKPointAnnotation alloc] init]; 
annot4.title = @"C"; 
annot4.subtitle=@"C1"; 
annot4.coordinate = CLLocationCoordinate2DMake(22.008867, 120.743637); 
[mapView addAnnotation:annot4]; 
MKPointAnnotation ***strong text**annot5 = [[MKPointAnnotation alloc] init]; 
annot5.title = @"D"; 
annot5.subtitle=@"D1"; 
annot5.coordinate = CLLocationCoordinate2DMake(22.016190, 120.837601); 
[mapView addAnnotation:annot5]; 
MKPointAnnotation *annot6 = [[MKPointAnnotation alloc] init]; 
annot6.title = @"E"; 
annot6.subtitle=@"E1"; 
annot6.coordinate = CLLocationCoordinate2DMake(22.024183, 120.743401); 
[mapView addAnnotation:annot6]; 
MKPointAnnotation *annot7 = [[MKPointAnnotation alloc] init]; 
annot7.title = @"F"; 
annot7.subtitle=@"F1"; 
annot7.coordinate = CLLocationCoordinate2DMake(22.055653, 121.509689); 
[mapView addAnnotation:annot7]; 
MKPointAnnotation *annot8 = [[MKPointAnnotation alloc] init]; 
annot8.title = @"G"; 
annot8.subtitle=@"G2"; 
annot8.coordinate = CLLocationCoordinate2DMake(22.070082, 120.713684); 
[mapView addAnnotation:annot8]; 

{

【问题讨论】:

  • 您在使用 ARC 吗?如果您不使用 ARC,则需要在将对象添加到 mapView 后释放它们。
  • 尝试 ARC 后,同样的事情仍然发生。我在哪里释放它?我在 ViewDidLoad 中有注释。如果您需要更多信息,请告诉我。 @弗雷迪
  • 你读过memory management rules吗?此外,该代码非常重复,您可能需要考虑将数据与创建注释的代码分开。
  • 感谢 Georg,我会继续阅读的。

标签: ios cocoa-touch memory-leaks mapkit


【解决方案1】:

如果您不使用 ARC,则应在将对象添加到地图视图后释放该对象。

例如:

MKPointAnnotation *annot1 = [[MKPointAnnotation alloc] init]; 
annot1.title = @"A"; 
annot1.subtitle=@"A1"; 
annot1.coordinate = CLLocationCoordinate2DMake(21.978954, 120.752663); 
[mapView addAnnotation:annot1]; 

应更新为:

MKPointAnnotation *annot1 = [[MKPointAnnotation alloc] init]; 
annot1.title = @"A"; 
annot1.subtitle=@"A1"; 
annot1.coordinate = CLLocationCoordinate2DMake(21.978954, 120.752663); 
[mapView addAnnotation:annot1];
[annot1 release]

原因是您的对象引用计数从未达到零,并且该对象从未被释放。

MKPointAnnotation *annot1 = [[MKPointAnnotation alloc] init];

当您分配一个对象时,它的引用计数为 1。如果您将一个对象添加到数组或字典中,则引用计数会增加。因此,在以下代码块之后,您的引用计数为 2。

MKPointAnnotation *annot1 = [[MKPointAnnotation alloc] init]; 
annot1.title = @"A"; 
annot1.subtitle=@"A1"; 
annot1.coordinate = CLLocationCoordinate2DMake(21.978954, 120.752663); 
[mapView addAnnotation:annot1]

现在,如果您在将 annot1 添加到 mapview 之后调用 release ,则该对象还没有真正释放。这是因为你的 mapview 中的数据结构持有对它的引用。

[mapView addAnnotation:annot1]

一旦你完成了你的 mapview 并且它被释放了,那么 annot1 最终被销毁了。

【讨论】:

  • 谢谢乔治,完全忽略了这一点。
  • 但是当我使用 ARC 时,同样的事情仍然会发生@Freddy
  • 如果你启用了 ARC,它不应该让你编译。当您启用 ARC 时,[object release] 会导致错误消息。确保释放您“分配”的每个对象。
  • 好的,我认为添加版本会有所帮助。非常感谢。我确实释放了所有 alloc 期望这个注解的东西。但是除了像这样对所有 2434 注释进行硬编码之外,还有什么建议吗? MKPointAnnotation *annot1 = [[MKPointAnnotation alloc] init]; annot1.title = @"A"; annot1.subtitle=@"A1"; annot1.coordinate = CLLocationCoordinate2DMake(21.978954, 120.752663); [mapView addAnnotation:annot1]; [annot1 发布] @Freddy
  • 如果你有一堆坐标需要硬编码,我会考虑硬编码一个 .plist。然后,当您构建地图视图时,只需读取 plist 文件并遍历每个坐标。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-06
相关资源
最近更新 更多