2009-09-03 9 views
1

非常に複雑なWebサービスが煮沸され、シンプルなコードまで検索されています。 検索(またはボタンをクリックすると、以下のサンプル)に応じて地図に注釈を追加できるようにする必要があります。次に、ユーザーがボタンをもう一度クリックして新しい結果セットを取得できるようにする必要があります。実際には異なる数字がありますが、単純化した例では、常に1つの注釈をマップビューに追加しています。 私のコードでは、既存の注釈を削除して新しい注釈を追加する必要がありますが、2回目以降のボタン押しで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

mymarkstoreLocationInfo:title:subtitle:index:に公開していません。問題は入力エラーであるようです。

[MyMark release]; 

を読み取るラインが

[mymark release]; 

注ケース差であるべきです。最初の行は、インスタンスではなくクラスにreleaseを送信します。

+0

あなたは正しいです。頭を叩く。かなりきつい。 – Andiih

関連する問題