1

以及我的问题“删除MKMapView注释会导致泄漏”。我发现如果你创建一个基于视图的项目,在视图的 NIB 中添加一个UISearchBarMKMapView,连接委托(我没有创建任何方法,因为我们实际上不需要做任何事情来触发泄漏),链接MapKit 并启动项目,然后只需单击UISearchBar导致 1k+ 泄漏。这不会发生,除非您同时拥有UISearchBarMKMapView在一个视图中。从代码创建视图时,我遇到了同样的问题。我认为 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 的子视图,但这可能没有必要复制这个问题。

在在这里发布之前测试此代码时,我刚刚注意到在模拟器中不会发生这种泄漏 - 仅在我的手机上......

4

2 回答 2

2

是的。

3.0 的 MKMapViews 上有一个已知的泄漏。当您取消分配 MKMapView 时会发生泄漏 这在以后的版本中已修复。解决方法是拥有一个 MKMapView 并重用它。

https://devforums.apple.com/message/129740#129740

于 2009-11-04T07:28:15.303 回答