2011-01-17 18 views
1

DisplayMap.h誰かがこのコードのリークを指摘できますか?

#import <Foundation/Foundation.h> 
#import <MapKit/MKAnnotation.h> 


@interface DisplayMap : NSObject <MKAnnotation> { 

    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subtitle; 
} 
@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic, copy) NSString *title; 
@property (nonatomic, copy) NSString *subtitle; 

@end 

DisplayMap.m

#import "DisplayMap.h" 


@implementation DisplayMap 

@synthesize coordinate,title,subtitle; 


-(void)dealloc{ 
    [title release]; 
    [super dealloc]; 
} 

@end 

私は注釈を表示するには、マップビューで上記を実装しています。 viewdidload上で、私は座標のセットを実行し、上記の注釈クラスを使用してマップ上に表示します。

for(int i=0;i<[xmlParameter count];i++){ 
    region.center.latitude=(double)[[[xmlParameter objectAtIndex:i]objectAtIndex:3] doubleValue]; 
    region.center.longitude =(double) [[[xmlParameter objectAtIndex:i]objectAtIndex:4] doubleValue] ; 
    region.span.longitudeDelta = 0.08f; 
    region.span.latitudeDelta = 0.08f; 
    DisplayMap *ann = [[DisplayMap alloc] init]; 
    ann.title = [[xmlParameter objectAtIndex:i]objectAtIndex:0]; 
    ann.subtitle = [[xmlParameter objectAtIndex:i]objectAtIndex:1]; 
    ann.coordinate = region.center; 
    [mapView addAnnotation:ann]; 
    if(i==zoomtoParameter){ 
     [mapView setRegion:region animated:YES];    
     //showAnnotation=ann;  
     [mapView selectAnnotation:currentAnnotation animated:YES];   
     //[mapView selectAnnotation:ann animated:YES]; 
    } 

    [ann release]; 
} 

リークのある計測器で動作すると、viewDidLoadメソッドの32ByteのDisplayMapリークがあると言われています。私は方法を理解することはできません。私はそれを行った直後にDisplayMapオブジェクトを解放しています。

提案がありますか?

おかげ

+0

あなたの 'dealloc'に' subtitle'も公開してみてください。 – Mahesh

+0

あなたはタイトルを公開しましたが、どこに割り当てましたか? –

答えて

4

あなたsubtitleプロパティは、あなたがそれを解放する責任がある意味copy属性、と宣言しています。詳述すると:

-(void)dealloc{ 
    [subtitle release]; 
    [title release]; 
    [super dealloc]; 
} 

編集:あなたのdeallocメソッドを次のように変更がトリックを行う必要がありますCocoaのmemory managementルールをあなたallocretainまたはcopy任意のメモリをreleaseしなければならないと述べています。合成プロパティの場合は、-deallocメソッドに適切なreleaseメッセージを含める必要があります。詳細については、このトピックに関する私自身のquestionを参照してください。

ann.subtitle = [[xmlParameter objectAtIndex:i]objectAtIndex:1]; 

が指示されたオブジェクトのコピーを作成します:あなたは、次の行を提供されているサンプルコードで

。後で[ann release]を呼び出すと、明示的に解放しない限り、そのコピーされたオブジェクトはリークされます。

+0

'[ann release];はすべてのメンバ変数の割り当てを解除しますか? – Mahesh

+0

eJamesが正しいです。あなたが[super dealloc]を見逃したとき、あなたは警告を受ける。 。 – nacho4d

+0

@Mahesh: '-dealloc'メソッドは、そのオブジェクトの存続期間中の任意の時点で、割り当て、保持、またはコピーされた(まだリリースされていない)メンバ変数を解放する責任があります。 –

関連する問題