2010-07-27 6 views
0

사용자가지도의 모든 핀을 켜고 끌 수 있어야하는 iPhone 응용 프로그램을 작성 중입니다. 지도에있는 모든 핀을 넣으려면 내가 사용 : 그것은 한 번에 모든 핀을 제거하고 다시 추가 잘 작동MKAnnotations를 제거하는 중 EXC_BAD_ACCESS

-(void) removeAllPins { 
    NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:([_mapView.annotations count] - 1)]; 
    for (id annotation in _mapView.annotations) { 
     if (annotation != _mapView.userLocation) { 
      [toRemove addObject:annotation]; 
     } 
    } 

    [_mapView removeAnnotations:toRemove]; 
} 

:

-(void) putAllPins { 
    for (id key in myDictionary) { //A NSDictionary 
     NSArray *data = [myDictionary objectForKey:key]; 
     [self putPin: data]; 
    } 
    isShowingAllPins = TRUE; 
} 

-(CLLocationCoordinate2D) putPin:(NSArray *) data { 
    NSNumber *lat = [data objectAtIndex:0]; 
    NSNumber *lon = [data objectAtIndex:1]; 
    NSString *name = [data objectAtIndex:2]; 
    NSString *info = [data objectAtIndex:3]; 

    CLLocationCoordinate2D coords = {[lat doubleValue], [lon doubleValue]}; 
    MyMapAnnotation *annotation = [[MyMapAnnotation alloc] initWithCoordinate:coords andName:name andInformation:info]; 
    [_mapView addAnnotation:annotation]; 
    [annotation release]; 
    return coords; 
} 

내가 사용을 제거합니다. 하지만 두 번째로 제거하면 EXC_BAD_ACCESS 오류가 발생합니다. 주석 클래스에서 dealloc 메서드로 문제를 추적했지만 여전히 무엇을해야할지 모릅니다. 어떤 도움을 주셔서 감사합니다!

MyAnnotation.m : 여기

@implementation MyAnnotation 

@synthesize coordinate = _coordinate; 

-(id) initWithCoordinate:(CLLocationCoordinate2D)coordinate andName:(NSString *)name andInformation:(NSString *)info { 
    self = [super init]; 
    _coordinate = coordinate; 
    _name = name; 
    _info = info; 

    return self; 
} 

-(NSString *)title { 
    return [NSString stringWithFormat:@"PREFIX %@", _name]; 
} 

-(NSString *)subtitle { 
    return _info; 
} 

- (void)dealloc { 
    [_name release]; 
    [_info release]; 
    [super dealloc]; 
} 

@end 

답변

1

시도 팁 # 1

http://loufranco.com/blog/files/debugging-memory-iphone.html

객체가 실제로 출시되지 않도록 당신은 엑스 코드를 설정하고이 할당이 해제 된 객체를 해제하면 그들은 불평이나 어떤 다른 방법으로 그들에게 메시지를 보냅니다.

업데이트 : _name은 절대로 보존되지 않습니다. 그런 다음 릴리스하십시오. _info와 동일합니다. 그들은 속성을 유지하는 경우, 생성 된 설정 메시지 (즉, 유지)

+0

흠, _name 및 _info는 절대로 보존되지 않으며 이는 dealloc 메소드가 전혀 존재하지 않아야 함을 의미합니다. 그것을 제거한 후에 모든 것이 잘 작동합니다. 이것을 지적 해 주셔서 감사합니다! –

+0

정확하지 않습니다. 이러한 문자열이 클래스에서 할당 해제되지 않도록하는 것은 무엇입니까? 정상적인 것은 당신이 그들에게 메시지를 나중에 보내고 dealloc에서 해제하기를 원하면 그들을 유지하는 것입니다. 유지/릴리스의 균형을 유지해야합니다. –

1
[_mapView addAnnotation:annotation]; 
[annotation release]; 

어쩌면 당신을 해결할 수 있습니다, 그것을 시도는 "주석"을 해제하지 마십시오 problem.But 내가 사용하는 self._name를 사용할 필요가 이 "주석"을 엄밀히 발표하는 방법을 모르겠다. 내가 미래에 주석을 제어해야한다면, 공개 할 수 없다. 그게 누수가 될 것인가? 나는 모른다.