2012-02-15 8 views
0

私はこの注釈(約2400)の多くを持っていますが、ここに私の問題があります。'annot'に割り当てられて格納されているオブジェクトの潜在的なリーク

Iが得る次のエラー

「ANNOT2」

にライン81に割り当てられ 「annot1」線81に割り当てられたオブジェクトの 電位漏れに格納され、格納されたオブジェクトの

電位リークオブジェクトの

電位リークがライン81に割り当てられ 「annot3」ように

とに格納されます。ここに私のコードは次のとおりです。

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

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

など

{

+0

ARCを使用していますか? ARCを使用していない場合は、オブジェクトをmapViewに追加した後にオブジェクトを解放する必要があります。 – Freddy

+0

ARCを試した後も同じことが起こります。どこから解放するのですか? ViewDidLoadに注釈があります。追加情報が必要な場合はお知らせください。 @Freddy –

+0

[メモリ管理ルール](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html)を読んだことはありますか?また、そのコードは非常に反駁的なので、アノテーションを作成するコードとはデータを分離しておくことを検討することをおすすめします。 –

答えて

1

あなたがあなたのMapViewにそれを追加した後、あなたがオブジェクトを解放すべきであるARCを使用していない場合。例えば

:理由は、あなたのオブジェクトの参照カウントがゼロにヒットしたことがないし、オブジェクトが解放されることはありませんということです

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

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

はに更新する必要があります。

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

オブジェクトを割り当てるとき、参照カウントは1です。オブジェクトを配列または辞書に追加すると、参照カウントが増分されます。したがって、次のコードブロックの後に参照カウントが2になります。あなたがあなたのMapViewにそれを追加した後annot1にリリースを呼び出す場合

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

さて、オブジェクトは実際にはまだリリースされていません。これは、あなたのマップビュー内のデータ構造が参照を保持しているからです。

[mapView addAnnotation:annot1] 

マップビューを終了してリリースされると、annot1は最終的に破棄されます。

+0

Georgeさんに感謝します。 – Freddy

+0

しかし、私がARCを使うと、同じことがまだ起こります@Freddy –

+0

ARCが有効になっているとコンパイルできません。 [オブジェクトのリリース]により、ARCが有効になっているときにエラーメッセージが表示されます。 "alloc"するオブジェクトを必ずリリースしてください。 – Freddy

関連する問題