2011-11-18 80 views
3

我一直在試圖擺脫mapview中的內存泄漏。我正在使用自定義地圖別針類。 一切正常,但問題是 - 我需要過濾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

我已經使用由MAYUR Birari,其中我調整一點點,創建對自定義地圖銷支持自定義地圖引腳圖像和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; 

在一個循環中我成立回覆quired價值和創建地圖圖釘:

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]; 

設置自定義圖像:上標註

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

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

獲取針ID:

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

    int tagNumber = v.tag_number; 

    .... 
} 

最後 - 在我的項目它需要有過濾器按鈕 - 所以我需要刪除所有引腳,並添加所需的。默認情況下調用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]; 
}