views:

740

answers:

2

As well as my question "Removing MKMapView Annotations causes leaks." I have discovered that if you create a view based project, add a UISearchBar and MKMapView into the view's NIB, wire up the delegates (I'm not creating any methods as we don't actually need to do anything to trigger the leaks), link in the MapKit and fire up the project, then simply clicking in the UISearchBar causes a 1k+ leak. This doesn't happen unless you have both UISearchBar and MKMapView in a view. I have the same issues when creating the views from code. I thought a NIB might behave differently, but it doesn't.

Is MKMapView leaky, or am I doing something wrong.

To replicate the issue with code try the code below - I created a new "view based application" project

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];
}

Although I've retained the subviews with mapView and searchBar, this is probably unnecessary to replicate the issue.

In testing this code prior to publishing here I've just noticed that this leak does not occur in the simulator - only on my phone...

+2  A: 

Yes.

There is a known leaks on 3.0's MKMapViews. The leak occurs when you deallocate the MKMapView This is fixed in later releases. The workaround is to have a single MKMapView and reuse it.

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

Roger Nolan
A: 

can u explain here for those who can't enter apple forum please... thx