Iphone 删除/向mapview添加注释会导致内存泄漏
我一直在试图消除mapview中的内存泄漏。我正在使用自定义地图pin类。 一切正常,但问题是-我需要过滤mapview结果。当我删除所有mapview注释并添加过滤结果时,性能工具会发现漏洞。但是在这个mapPin类中,我使用的是autorelease,所以它们应该被释放,但它们不是。我做错了什么 MapPin.hIphone 删除/向mapview添加注释会导致内存泄漏,iphone,annotations,mkmapview,Iphone,Annotations,Mkmapview,我一直在试图消除mapview中的内存泄漏。我正在使用自定义地图pin类。 一切正常,但问题是-我需要过滤mapview结果。当我删除所有mapview注释并添加过滤结果时,性能工具会发现漏洞。但是在这个mapPin类中,我使用的是autorelease,所以它们应该被释放,但它们不是。我做错了什么 MapPin.h #import <Foundation/Foundation.h> #import <MapKit/MapKit.h> #import <MapKit
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import <MapKit/MKMapView.h>
#import <MapKit/MKAnnotation.h>
@interface MapPin : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString * picture;
NSInteger tag_number;
}
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord;
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title;
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title subtitle:(NSString *) subtitle;
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title subtitle:(NSString *) subtitle image:(NSString *) pic;
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title subtitle:(NSString *) subtitle image:(NSString *) pic num:(NSInteger) number;
- (void) setPic:(NSString *) picture;
- (NSString* ) getPic;
- (void) setNum:(NSInteger) tag_number;
- (NSInteger ) getNum;
@end
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface CustomMapPin : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString* title;
NSString* subtitle;
NSString* pic;
NSInteger tag_number;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString* title;
@property (nonatomic, copy) NSString* subtitle;
@property (nonatomic, copy) NSString* pic;
@property (nonatomic) NSInteger tag_number;
@end
我一直在使用由Mayur Birari创建的cutom map pin,来支持自定义地图pin图像和id CustomMapPin.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import <MapKit/MKMapView.h>
#import <MapKit/MKAnnotation.h>
@interface MapPin : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString * picture;
NSInteger tag_number;
}
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord;
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title;
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title subtitle:(NSString *) subtitle;
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title subtitle:(NSString *) subtitle image:(NSString *) pic;
- (id) initWithCoordinate:(CLLocationCoordinate2D) coord title:(NSString *) title subtitle:(NSString *) subtitle image:(NSString *) pic num:(NSInteger) number;
- (void) setPic:(NSString *) picture;
- (NSString* ) getPic;
- (void) setNum:(NSInteger) tag_number;
- (NSInteger ) getNum;
@end
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface CustomMapPin : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString* title;
NSString* subtitle;
NSString* pic;
NSInteger tag_number;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString* title;
@property (nonatomic, copy) NSString* subtitle;
@property (nonatomic, copy) NSString* pic;
@property (nonatomic) NSInteger tag_number;
@end
在课堂上使用它,就像这样:
CLLocationCoordinate2D pinlocation;
在一个循环中,我设置了所需的值并创建了一个映射接点:
设置自定义映像:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
if ([annotation isKindOfClass: [CustomMapPin class]])
{
CustomMapPin * a = annotation;
[annView setImage:[UIImage imageNamed:a.pic]];
}
}
最后,在我的项目中,它需要有过滤器按钮,所以我需要删除所有的引脚,并添加所需的。默认情况下,调用mapview以删除由内存泄漏创建的所有注释。因此,当我需要从注释中清除mapview时,我调用此函数:
- (void)removeAnnotations
{
NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:[mapView.annotations count]];
for (id annotation in mapView.annotations)
{
if (annotation != mapView.userLocation)
{
[toRemove addObject:annotation];
}
}
[mapView removeAnnotations:toRemove];
for(int i = 0; i < [toRemove count]; i++)
{
CustomMapPin * a = [toRemove objectAtIndex:i];
[a release];
a = nil;
}
}
-(无效)删除注释
{
NSMutableArray*toRemove=[NSMutableArray阵列容量:[mapView.annotations count]];
用于(mapView.annotations中的id注释)
{
if(注释!=mapView.userLocation)
{
[删除添加对象:注释];
}
}
[地图视图删除说明:删除];
对于(int i=0;i<[toRemove count];i++)
{
CustomMapPin*a=[toRemove objectAtIndex:i];
[释放];
a=零;
}
}
希望这有帮助
快乐编码!:) 您刚刚错过了dealloc实现 例如:
- (void)dealloc
{
[self.title release];
self.title = nil;
self.subtitle release];
self.subtitle = nil;
[super dealloc];
}
我没有看到任何dealloc方法实现。你在使用ARC吗?没有ARC,只是在互联网上找到了这个代码,从那时起就一直在使用这个类。刚刚决定测试内存泄漏。您确定在从映射中删除所有注释后可以进行[发布]吗?这不是自动发布注释吗?
- (void)removeAnnotations
{
NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:[mapView.annotations count]];
for (id annotation in mapView.annotations)
{
if (annotation != mapView.userLocation)
{
[toRemove addObject:annotation];
}
}
[mapView removeAnnotations:toRemove];
for(int i = 0; i < [toRemove count]; i++)
{
CustomMapPin * a = [toRemove objectAtIndex:i];
[a release];
a = nil;
}
}
- (void)dealloc
{
[self.title release];
self.title = nil;
self.subtitle release];
self.subtitle = nil;
[super dealloc];
}