ios - 分配并存储到 'annot' 的对象存在潜在泄漏

标签 ios cocoa-touch memory-leaks mapkit

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

我收到以下错误

potential leak of an object allocated on line 81 and stored into 'annot1' potential leak of an object allocated on line 81 and stored into 'annot2'

potential leak of an object allocated on line 81 and stored into '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,那么您应该在将对象添加到 map View 后释放该对象。

例如:

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 添加到 map View 后调用release,则该对象尚未真正释放。这是因为 map View 中的数据结构保存了对它的引用。

[mapView addAnnotation:annot1]

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

关于ios - 分配并存储到 'annot' 的对象存在潜在泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9286782/

相关文章:

c++ - 带有局部变量声明的 dispatch_apply 不能在 C++ 方法实现中编译

ios - 使用 vb.net 将包含文件的文件夹导入到越狱的 iPhone 中

ios - 如何使用 Perform(aSelector : , with : , afterDelay : , inModes: ) 在延迟后暂停 CABasicAnimation

iphone - 使用背景图像自定义 UIBarbuttonitem

ios - 关闭一个 View 并从 applicationDidEnterBackground 中呈现一个新 View

java - 多次执行应用程序后内存不足

ios - 如何在带有 alpha 的 uiviewcontroller 上添加全屏 maskview

android - Activity 泄露了原本绑定(bind)的 ServiceConnection com.google.android.youtube.player.internal.r$e@43cb2348

python - 美丽汤内存泄漏

ios - iPhone 在后台收集 CoreMotion 数据。 (超过10分钟)