以及我的问题“删除MKMapView
注释会导致泄漏”。我发现如果你创建一个基于视图的项目,在视图的 NIB 中添加一个UISearchBar
和MKMapView
,连接委托(我没有创建任何方法,因为我们实际上不需要做任何事情来触发泄漏),链接MapKit 并启动项目,然后只需单击UISearchBar
导致 1k+ 泄漏。这不会发生,除非您同时拥有UISearchBar
和MKMapView
在一个视图中。从代码创建视图时,我遇到了同样的问题。我认为 NIB 的行为可能会有所不同,但事实并非如此。
是MKMapView
泄漏,还是我做错了什么。
要使用代码复制问题,请尝试以下代码 - 我创建了一个新的“基于视图的应用程序”项目
TestMapViewFromCodeViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface TestMapViewFromCodeViewController : UIViewController {
UISearchBar *searchBar;
MKMapView *mapView;
}
@property (nonatomic, retain) MKMapView *mapView;
@property (nonatomic, retain) UISearchBar *searchBar;
@end
TestMapViewFromCodeViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
UISearchBar * tmpSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0,0.0,self.view.frame.size.width,40.0)];
[self.view addSubview:tmpSearchBar];
[self setSearchBar:tmpSearchBar];
[tmpSearchBar release];
MKMapView *tmpMapView=[[MKMapView alloc] initWithFrame:CGRectMake(0.0,0.0,self.view.frame.size.width,self.view.frame.size.height)];
tmpMapView.showsUserLocation=FALSE;
[self.view insertSubview:tmpMapView atIndex:0];
[self setMapView:tmpMapView];
[tmpMapView release];
}
- (void)dealloc {
[mapView release];
[searchBar release];
[super dealloc];
}
虽然我保留了带有 mapView 和 searchBar 的子视图,但这可能没有必要复制这个问题。
在在这里发布之前测试此代码时,我刚刚注意到在模拟器中不会发生这种泄漏 - 仅在我的手机上......