Утечки памяти MapKit. Не знаю почему - PullRequest
2 голосов
/ 27 июля 2011

Я использую инструмент утечки в инструментах на устройстве.Я получаю утечки MKReverseGeocoder, MKReverseGeocoderInternal и NSCFString (указывает на - (void) reverseGeocoder: didFindPlacemark).Я работаю над этими утечками более 2 дней и не могу найти решение.Если бы кто-нибудь мог указать мне правильное направление, я был бы очень любезен.

.H

@interface ReviewViewController : UIViewController <MKMapViewDelegate, MKReverseGeocoderDelegate,MKAnnotation > {
    IBOutlet UIImageView *_changingStationIV;
    IBOutlet UIImageView *_feedingAreaIV;
    IBOutlet UIImageView *_highChairIV;
    IBOutlet MKMapView *_mapView;
    MKReverseGeocoder *_reverseGeocoder;
    SGFeature *_place;
    MKPlacemark *_loc;

}

@property(nonatomic,retain)IBOutlet UIImageView *changingStationIV;
@property(nonatomic,retain)IBOutlet UIImageView *feedingAreaIV;
@property(nonatomic,retain)IBOutlet UIImageView *highChairIV;
@property(nonatomic,retain)IBOutlet MKMapView *mapView;
@property(nonatomic,retain) MKReverseGeocoder *reverseGeocoder;
@property(nonatomic,retain) SGFeature *place;
@property(nonatomic,retain) MKPlacemark *loc;

@end

.M

        @implementation ReviewViewController
     @synthesize changingStationIV=_changingStationIV,feedingAreaIV=_feedingAreaIV, highChairIV= _highChairIV, reverseGeocoder= _reverseGeocoder, mapView=_mapView,loc=_loc,coordinate=_coordinate,place=_place;

     // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
        [super viewDidLoad];
        NSLog(@"ViewDidLoad");

    NSLog(@"CURRENT PLACE %@",  [[self.place asDictionary] description]);

    NSDictionary *props = [self.place properties];

    self.title = [props objectForKey:@"name"];

    NSLog(@"changingStation: %@", [props objectForKey:@"changingStation"]);

    NSLog(@"feedingArea: %@", [props objectForKey:@"feedingArea"]);

    NSLog(@"highChair: %@", [props objectForKey:@"highChair"]);

    if ([[props objectForKey:@"changingStation"] boolValue]==YES) {
        self.changingStationIV.image = [UIImage imageNamed:@"check.png"];
    }else {
        self.changingStationIV.image = [UIImage imageNamed:@"x.png"];
    }
    if ( [[props objectForKey:@"feedingArea"] boolValue]==YES) {
        self.feedingAreaIV.image = [UIImage imageNamed:@"check.png"];
    }else {
        self.feedingAreaIV.image = [UIImage imageNamed:@"x.png"];
    }
    if ( [[props objectForKey:@"highChair"] boolValue]==YES) {
        self.highChairIV.image = [UIImage imageNamed:@"check.png"];
    }else {
        self.highChairIV.image = [UIImage imageNamed:@"x.png"];
    }

    SGPoint *point = (SGPoint*)[self.place geometry];
    CLLocationCoordinate2D locCoord = CLLocationCoordinate2DMake(point.latitude, point.longitude);
    NSDictionary *locDict = [NSDictionary dictionaryWithObject:[props objectForKey:@"name"] forKey:@"Country"];
    self.loc = [[MKPlacemark alloc] initWithCoordinate:locCoord addressDictionary:locDict];
    [self.mapView addAnnotation:self.loc];


    MKUserLocation *userLocation = self.mapView.userLocation;
    CLLocationCoordinate2D coord = userLocation.location.coordinate;
    [self.mapView setCenterCoordinate:coord animated:YES];

    MKCoordinateRegion region;
    MKCoordinateSpan span;
    region.center = locCoord;
    span.latitudeDelta =0.005334;
    span.longitudeDelta = 0.011834;
    region.span = span;

    [self.mapView setCenterCoordinate:locCoord animated:YES];
    [self.mapView setRegion:region animated:YES];


        self.reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:locCoord];
        self.reverseGeocoder.delegate = self;
            [self.reverseGeocoder start];



    }


        -(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{

        }

        -(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{

                self.mapView.userLocation.title = placemark.title;

        }   


        - (void)didReceiveMemoryWarning {
            // Releases the view if it doesn't have a superview.
            [su

per didReceiveMemoryWarning];

        // Release any cached data, images, etc. that aren't in use.
    }

    - (void)viewDidUnload {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;

    NSLog(@"ViewDidUnload");


        self.changingStationIV =nil;
        self.feedingAreaIV=nil;
        self.highChairIV=nil;
        self.reverseGeocoder=nil;
        self.place=nil;
        self.loc=nil;


    }


    - (void)dealloc {

        NSLog(@"Review-Dealloc");


        self.reverseGeocoder=nil;
        self.mapView=nil;
        self.changingStationIV =nil;
        self.feedingAreaIV=nil;
        self.highChairIV=nil;
        self.place=nil;
        self.loc=nil;


        [super dealloc];

    }


    @end

Ответы [ 2 ]

3 голосов
/ 27 июля 2011

Вы, безусловно, теряете память здесь:

    self.loc = [[MKPlacemark alloc] initWithCoordinate:locCoord 
                                     addressDictionary:locDict];

Поскольку вы определили loc как свойство retain, присвоение ему вызовет retaing для аргумента, и вы закончитес MKPlacemark, который хранится и никогда не выпускается.

Я бы изменил эту строку следующим образом:

    self.loc = [[[MKPlacemark alloc] initWithCoordinate:locCoord 
                                     addressDictionary:locDict] autorelease];

Кроме того, тщательно проверьте ваш код для этого шаблона, так как @omzпредполагает, что вы делаете это также при назначении на reverseGeocoder.

2 голосов
/ 27 июля 2011

В вашем методе viewDidLoad вы устанавливаете свойство reverseGeocoder следующим образом:

self.reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:locCoord];

Это утечка, потому что установщик свойств уже сохраняет объект. Вам нужно освободить или автоматически выпустить экземпляр MKReverseGeocoder после установки свойства, например ::100100

self.reverseGeocoder = [[[MKReverseGeocoder alloc] initWithCoordinate:locCoord] autorelease];
...