我一直在試圖擺脫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
我沒有看到任何dealloc方法的實現。你在使用ARC嗎? – Denis
沒有ARC,只是在互聯網上找到這個代碼,並從那時起一直在使用這個類。剛剛決定測試內存泄漏。 – user979250