2011-11-18 20 views
3

私はmapviewでメモリリークを取り除こうとしています。私はカスタムマップピンクラスを使用しています。 すべて動作しますが、問題は - マップビューの結果をフィルタリングする必要があります。すべてのmapviewアノテーションを削除し、フィルタリングされた結果を追加すると、パフォーマンスツールがリークを検出します。しかし、私が使っているこのmapPinクラスでは、autoreleaseが使われているので、解放されるべきですが、そうではありません。私は何を間違えているのですか?mapviewにアノテーションを削除/追加するとメモリリークが発生する

MapPin.h

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

@interface MapPin : NSObject<MKAnnotation> { 
    CLLocationCoordinate2D coordinate; 
    NSString * picture; 
    NSInteger tag_number; 
} 

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


- (id) initWithCoordinate:(CLLocationCoordinate2D) coord; 
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title; 
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title subtitle:(NSString *) subtitle; 
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title subtitle:(NSString *) subtitle image:(NSString *) pic; 
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title subtitle:(NSString *) subtitle image:(NSString *) pic num:(NSInteger) number; 
- (void) setPic:(NSString *) picture; 
- (NSString*) getPic; 

- (void) setNum:(NSInteger) tag_number; 
- (NSInteger) getNum; 

@end 

MapPin.m

#import "MapPin.h" 


@implementation MapPin 

@synthesize coordinate = _coordinate; 
@synthesize title = _title; 
@synthesize subtitle = _subtitle; 


- (id) initWithCoordinate:(CLLocationCoordinate2D) coord 
{ 
    return [self initWithCoordinate:coord title:@""]; 
} 

- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title { 
    return [self initWithCoordinate:coord title:title subtitle:@""]; 

} 
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title subtitle:(NSString *) subtitle { 
    return [self initWithCoordinate:coord title:title subtitle:subtitle image:@""];} 

- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title subtitle:(NSString *) subtitle image:(NSString *) pic{ 
    MapPin * me = [[[MapPin alloc] init] autorelease]; 
    me.coordinate = coord; 
    me.title = title; 
    me.subtitle = subtitle; 

    [me setPic:pic]; 

    return me; 
} 

- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title subtitle:(NSString *) subtitle image:(NSString *) pic num:(NSInteger) number{ 
    MapPin * me = [[[MapPin alloc] init] autorelease]; 
    me.coordinate = coord; 
    me.title = title; 
    me.subtitle = subtitle; 

    [me setPic:pic]; 
    [me setNum:number]; 
    return me; 
} 

- (void) setPic:(NSString*) pic { 
    picture = pic; 
} 

- (NSString *) getPic{ 
    return picture; 
} 

- (void) setNum:(NSInteger) number { 
    tag_number = number; 

} 

- (NSInteger) getNum{ 
    return tag_number; 
} 

@end 
+1

私はdeallocメソッドの実装を見ません。 ARCを使用していますか? – Denis

+1

ARCはありません。インターネットでこのコードが見つかりました。このクラスを使用しています。今すぐメモリリークをテストすることに決めました。 – user979250

答えて

2

私はに、私は少し微調整しマユールBirari、によって作成されたcutomマップのピンを使用していますカスタムマップのピン画像とIDをサポートします。

CustomMapPin.h

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

@interface CustomMapPin : NSObject<MKAnnotation> { 

    CLLocationCoordinate2D coordinate; 
    NSString*    title; 
    NSString*    subtitle; 

    NSString*    pic; 
    NSInteger    tag_number; 

} 

@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic, copy)  NSString*    title; 
@property (nonatomic, copy)  NSString*    subtitle; 
@property (nonatomic, copy)  NSString*    pic; 
@property (nonatomic)   NSInteger    tag_number; 


@end 

CustomMapPin.m

#import "CustomMapPin.h" 


@implementation CustomMapPin 

@synthesize title; 
@synthesize subtitle; 
@synthesize coordinate; 
@synthesize pic; 
@synthesize tag_number; 


- (void)dealloc 
{ 
    self.title = nil; 
    self.pic = nil; 
    self.subtitle = nil; 
    [super dealloc]; 
} 

@end 

と、このようなクラスでそれを使用して:私は設定したループで

CLLocationCoordinate2D pinlocation; 

再それが必要だった私のプロジェクトに -

- (void)mapView:(MKMapView *)mp annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 
    CustomMapPin * v = (CustomMapPin *) view.annotation; 

    int tagNumber = v.tag_number; 

    .... 
} 

し、最終的に:コールアウトの

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation 
{ 
    if ([annotation isKindOfClass: [CustomMapPin class]]) 
    { 
     CustomMapPin * a = annotation; 

     [annView setImage:[UIImage imageNamed:a.pic]]; 
    } 
} 

取得ピン番号:

pinlocation.latitude = ...; 

pinlocation.longitude = ...; 

NSInteger pinID = ....; 


CustomMapPin* customMapPin=[[CustomMapPin alloc] init]; 

customMapPin.coordinate=(CLLocationCoordinate2D 
    {pinlocation.latitude,pinlocation.longitude}; 

[email protected]"title"; 

[email protected]"subtitle"; 

customMapPin.pic = @"customImageName"; 

customMapPin.tag_number = pinId; 


[mapView addAnnotation:customMapPin]; 

は、カスタムイメージの設定:quired値とは、マップのピンを作成しますフィルタボタンを用意する必要があります。そのため、すべてのピンを削除し、必要なものを追加する必要がありました。デフォルトでは、すべての注釈を削除するためにmapviewを呼び出すとメモリリークが発生しました。私は注釈からのMapViewをクリアする必要がある場合ので、私はこの関数を呼び出す:

- (void)removeAnnotations 
{ 
    NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:[mapView.annotations count]]; 

    for (id annotation in mapView.annotations) 
    { 
     if (annotation != mapView.userLocation) 
     { 
      [toRemove addObject:annotation]; 
     } 
    } 

    [mapView removeAnnotations:toRemove]; 

    for(int i = 0; i < [toRemove count]; i++) 
    { 
     CustomMapPin * a = [toRemove objectAtIndex:i]; 

     [a release]; 

     a = nil; 
    } 
} 

希望これは コーディングハッピーに役立ちます! :)

+0

地図からすべての注釈を削除した後に[リリース]をすることはできますか?注釈を自動的にリリースするものではないのですか? – AmitP

2

あなたはdealloc実装を見逃していました!例えば

は:

- (void)dealloc 
{ 
    [self.title release]; 

    self.title = nil; 

    self.subtitle release]; 

    self.subtitle = nil; 

    [super dealloc]; 
} 
関連する問題