Removing MKMapView Annotations causes leaks
我将一组非常复杂的Web服务和搜索归结为简单的以下代码。我需要能够在地图中添加注释以响应搜索(或者在下面的示例中单击按钮),然后允许用户再次单击按钮并获得一组新的结果。实际上会有一个不同的数字,但是在这个简化的示例中,我们总是向地图视图添加一个注释。我相信我的代码应该删除现有的注释并添加一个新的注释,但是它会在第二次和随后的按钮按下时泄漏32个字节。我错过了什么?(或视情况保留!)
测试视图控制器.h
1 2 3 4 5 6 7 8 9 | #import <UIKit/UIKit.h> #import <MapKit/MapKit.h> #import"MyMark.h" @interface testViewController : UIViewController { MKMapView *mapView; } @end |
测试视图控制器.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { // Custom initialization self.title=@"test"; } return self; } - (void) storeLocationInfo: (CLLocationCoordinate2D) loc title:(NSString *)t subtitle:(NSString *)st index:(int)i { NSArray * annotations = [mapView annotations]; [mapView removeAnnotations:annotations]; MyMark * mymark=[[MyMark alloc] initWithCoordinate:loc]; [mapView addAnnotation:mymark]; [MyMark release]; } - (void)viewDidLoad { [super viewDidLoad]; UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"Add point to map" style:UIBarButtonItemStylePlain target:self action:@selector(addPushed)]; [self.navigationItem setRightBarButtonItem:barButton]; [barButton release]; mapView=[[MKMapView alloc] initWithFrame:CGRectMake(0.0,0.0,self.view.frame.size.width,self.view.frame.size.height)]; mapView.showsUserLocation=FALSE; mapView.delegate=self; [self.view insertSubview:mapView atIndex:0]; [mapView release]; } - (void) addPushed { MKCoordinateRegion reg = mapView.region; [self storeLocationInfo:reg.center title:@"price" subtitle:@"title" index:1]; } - (void)dealloc { [super dealloc]; } |
麦克马克
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface MyMark : NSObject<MKAnnotation> { CLLocationCoordinate2D coordinate; NSString * title; NSString * subtitle; int index; } @property (nonatomic, readonly) CLLocationCoordinate2D coordinate; @property (nonatomic, readonly) int index; @property (nonatomic, retain) NSString * title; @property (nonatomic, retain) NSString * subtitle; -(id)initWithCoordinate:(CLLocationCoordinate2D) coordinate; -(id)setCoordinate:(CLLocationCoordinate2D) coordinate; -(id)setTitle:(NSString *)t subtitle:(NSString *)st index:(int)i ; @end |
迈克马克
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #import"MyMark.h" @implementation MyMark @synthesize coordinate, index; @synthesize title,subtitle; -(id)initWithCoordinate:(CLLocationCoordinate2D) c{ coordinate=c; NSLog(@"%f,%f",c.latitude,c.longitude); return self; } -(id)setCoordinate:(CLLocationCoordinate2D) c{ coordinate=c; NSLog(@"%f,%f",c.latitude,c.longitude); return self; } -(id)setTitle:(NSString *)t subtitle:(NSString *)st index:(int) i{ self.title=t; self.subtitle=st; index=i; return self; } -(void) dealloc { [title release]; [subtitle release]; [super dealloc]; } |
你不会在
1 | [MyMark release]; |
应该是
1 | [MyMark release]; |
注意大小写的区别。第一行将