2009-09-03 79 views
1

我煮了一組非常複雜的Web服務,並搜索到簡單的下面的代碼。 我需要能夠向地圖添加註釋以響應搜索(或者在下面的示例中點擊按鈕),然後允許用戶再次單擊該按鈕並獲得一組新結果。實際上會有不同的數字,但在簡單的例子中,我們總是在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

您不會在storeLocationInfo:title:subtitle:index:中發佈mymark。它看起來像問題是一個打字錯誤。讀取

[MyMark release]; 

該行應

[mymark release]; 

注意的情況不同。第一行將release發送給班級,而不是實例。

+0

OMG你是對的。自拍頭。挺難的。 – Andiih 2009-10-01 08:19:48