2009-09-03 3 views
1

Я приготовил очень сложный набор веб-сервисов и выполнил поиск до простого следующего кода. Мне нужно иметь возможность добавлять аннотации к карте в ответ на поиск (или в примере ниже до щелчка кнопки), а затем разрешить пользователю снова нажимать кнопку и получать новый набор результатов. На самом деле будет другое число, но в упрощенном примере мы всегда добавляем одну аннотацию в mapview. Я считаю, что мой код должен удалить существующие аннотации и добавить новый, но он утечки 32 байт на второй и последующих нажатиях кнопок. Что мне не хватает? (Или сохранение в зависимости от обстоятельств может быть!)Удаление MKMapView Аннотации приводят к утечкам

testViewController.h

 
#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 
#import "MyMark.h" 

@interface testViewController : UIViewController { 
    MKMapView *mapView; 
} 

@end 

testViewController.m

 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 
     // Custom initialization 
     [email protected]"test"; 
    } 
    return self; 
} 

- (void) storeLocationInfo: (CLLocationCoordinate2D) loc title:(NSString *)t subtitle:(NSString *)st index:(int)i { 
    NSArray * annotations = [mapView annotations]; 
    [mapView removeAnnotations:annotations]; 

    MyMark * mymark=[[MyMark alloc] initWithCoordinate:loc]; 
    [mapView addAnnotation:mymark]; 
    [MyMark release]; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"Add point to map" style:UIBarButtonItemStylePlain target:self action:@selector(addPushed)]; 
    [self.navigationItem setRightBarButtonItem:barButton]; 
    [barButton release]; 

    mapView=[[MKMapView alloc] initWithFrame:CGRectMake(0.0,0.0,self.view.frame.size.width,self.view.frame.size.height)]; 
    mapView.showsUserLocation=FALSE; 
    mapView.delegate=self; 
    [self.view insertSubview:mapView atIndex:0]; 
    [mapView release]; 
} 

- (void) addPushed { 
    MKCoordinateRegion reg = mapView.region; 
    [self storeLocationInfo:reg.center title:@"price" subtitle:@"title" index:1]; 
} 

- (void)dealloc { 
    [super dealloc]; 
} 

MyMark.h

 
#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 


@interface MyMark : NSObject<MKAnnotation> { 
    CLLocationCoordinate2D coordinate; 
    NSString * title; 
    NSString * subtitle; 
    int index; 
} 
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 
@property (nonatomic, readonly) int index; 
@property (nonatomic, retain) NSString * title; 
@property (nonatomic, retain) NSString * subtitle; 
-(id)initWithCoordinate:(CLLocationCoordinate2D) coordinate; 
-(id)setCoordinate:(CLLocationCoordinate2D) coordinate; 
-(id)setTitle:(NSString *)t subtitle:(NSString *)st index:(int)i ; 

@end 

MyMark.m

 
#import "MyMark.h" 


@implementation MyMark 
@synthesize coordinate, index; 
@synthesize title,subtitle; 

-(id)initWithCoordinate:(CLLocationCoordinate2D) c{ 
    coordinate=c; 
    NSLog(@"%f,%f",c.latitude,c.longitude); 
    return self; 
} 

-(id)setCoordinate:(CLLocationCoordinate2D) c{ 
    coordinate=c; 
    NSLog(@"%f,%f",c.latitude,c.longitude); 
    return self; 
} 

-(id)setTitle:(NSString *)t subtitle:(NSString *)st index:(int) i{ 
    self.title=t; 
    self.subtitle=st; 
    index=i; 
    return self; 
} 

-(void) dealloc { 
    [title release]; 
    [subtitle release]; 
    [super dealloc]; 
} 

ответ

4

Вы не выпускаете mymark в storeLocationInfo:title:subtitle:index:. Похоже, проблема заключается в ошибке ввода. Линия, которая читает

[MyMark release]; 

должен быть

[mymark release]; 

Примечание разница случай. Первая строка отправляет release классу, а не экземпляру.

+0

OMG Вы правы. Похлопывает себя по голове. Довольно трудно. – Andiih

 Смежные вопросы

  • Нет связанных вопросов^_^