【问题标题】:Removing MKMapView Annotations causes leaks删除 MKMapView 注释会导致泄漏
【发布时间】:2010-11-25 06:01:27
【问题描述】:

我将一组非常复杂的 Web 服务和搜索归结为以下简单的代码。 我需要能够向地图添加注释以响应搜索(或在下面的示例中单击按钮),然后允许用户再次单击按钮并获得一组新结果。实际上会有一个不同的数字,但在简化的示例中,我们总是向地图视图添加一个注释。 我相信我的代码应该删除现有的注释并添加一个新的注释,但是在第二次和随后的按钮按下时它会泄漏 32 个字节。 我错过了什么? (或视情况保留!)

testViewController.h

#import #import #import "MyMark.h" @interface testViewController : UIViewController { MKMapView *mapView; } @结尾

testViewController.m

- (id)initWithNibName:(NSString *)nibNameOrNil 包:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { // 自定义初始化 self.title=@"测试"; } 回归自我; } - (void) storeLocationInfo: (CLLocationCoordinate2D) loc title:(NSString *)t subtitle:(NSString *)st index:(int)i { NSArray * 注释 = [mapView 注释]; [mapView removeAnnotations:annotations]; MyMark * mymark=[[MyMark alloc] initWithCoordinate:loc]; [mapView addAnnotation:mymark]; [MyMark 发布]; } - (void)viewDidLoad { [超级视图DidLoad]; UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"添加点到地图" style:UIBarButtonItemStylePlain target:self action:@selector(addPushed)]; [self.navigationItem setRightBarButtonItem:barButton]; [barButton 释放]; mapView=[[MKMapView alloc] initWithFrame:CGRectMake(0.0,0.0,self.view.frame.size.width,self.view.frame.size.height)]; mapView.showsUserLocation=假; mapView.delegate=自己; [self.view insertSubview:mapView atIndex:0]; [地图视图发布]; } -(无效)addPushed { MKCoordinateRegion reg = mapView.region; [self storeLocationInfo:reg.center title:@"price" subtitle:@"title" index:1]; } - (无效)dealloc { [超级释放]; }

MyMark.h

#import #import @interface MyMark : NSObject { CLLocationCoordinate2D坐标; NSString * 标题; NSString * 字幕; 整数索引; } @property (nonatomic, readonly) CLLocationCoordinate2D 坐标; @property (nonatomic, readonly) int index; @property (nonatomic, 保留) NSString * title; @property (nonatomic, 保留) NSString * subtitle; -(id)initWithCoordinate:(CLLocationCoordinate2D) 坐标; -(id)setCoordinate:(CLLocationCoordinate2D) 坐标; -(id)setTitle:(NSString *)t subtitle:(NSString *)st index:(int)i ; @结尾

MyMark.m

#import "MyMark.h" @implementation MyMark @synthesize 坐标、索引; @synthesize 标题,副标题; -(id)initWithCoordinate:(CLLocationCoordinate2D) c{ 坐标=c; NSLog(@"%f,%f",c.latitude,c.longitude); 回归自我; } -(id)setCoordinate:(CLLocationCoordinate2D) c{ 坐标=c; NSLog(@"%f,%f",c.latitude,c.longitude); 回归自我; } -(id)setTitle:(NSString *)t subtitle:(NSString *)st index:(int) i{ self.title=t; self.subtitle=st; 指数=我; 回归自我; } -(无效)dealloc { [标题发布]; [字幕发布]; [超级释放]; }

【问题讨论】:

    标签: ios objective-c cocoa-touch memory-leaks mapkit


    【解决方案1】:

    你不会在storeLocationInfo:title:subtitle:index: 中释放mymark。看起来问题是打字错误。读取的行

    [MyMark release];
    

    应该是

    [mymark release];
    

    注意大小写差异。第一行将release 发送给类,而不是实例。

    【讨论】:

      猜你喜欢
      • 2012-01-01
      • 2012-06-16
      • 1970-01-01
      • 2012-08-10
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多