大家好! 我已經測試這個簡單的代碼如下:MKMapView removeAnnotations導致內存泄漏
StorePin.h
#import <Foundation/Foundation.h>
#import <MAPKIT/mapkit.h>
#import <CORELOCATION/corelocation.h>
@interface StorePin : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *subtitle;
NSString *title;
}
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
@property (nonatomic,retain) NSString *subtitle;
@property (nonatomic,retain) NSString *title;
-(id) initWithCoords:(CLLocationCoordinate2D) coords;
@end
StorePin.m
#import "StorePin.h"
@implementation StorePin
@synthesize coordinate, subtitle, title;
- (id) initWithCoords:(CLLocationCoordinate2D) coords{
self = [super init];
if (self != nil) {
coordinate = coords;
}
return self;
}
- (void) dealloc
{
[title release];
[subtitle release];
[super dealloc];
}
@end
在我ViewControlller,我做了一個按鈕來添加和刪除屢註解。
#import "mapViewTestViewController.h"
#import "StorePin.h"
@implementation mapViewTestViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)refresh
{
[mapView removeAnnotations:mapView.annotations];
for (int i = 0; i < 101; i ++)
{
CLLocationCoordinate2D p1;
p1.latitude = i/10.0;
p1.longitude = i/10.0;
StorePin *poi = [[StorePin alloc] initWithCoords:p1];
[mapView addAnnotation:poi];
[poi release];
}
}
- (void)dealloc
{
[super dealloc];
}
@end
如果我循環少於100次添加和刪除註釋,一切正常。但是如果我循環超過100次,會造成一次內存泄漏。我對這個奇怪的問題幾乎感到瘋狂。這是我的代碼的bug或mkmapview的錯誤?感謝你們對我的幫助。