Memory Leak Problem with MKMapViewDelegate method
The code I have is:
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"Pin"];
if(pinView == nil)
{
pinView = 开发者_开发知识库[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"];
pinView.annotation = annotation;
pinView.animatesDrop = YES;
[pinView setCanShowCallout:YES];
if([[(MapPin *)annotation title] isEqualToString:@"Starting Location"])
{
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.rightCalloutAccessoryView = nil;
}
else
{
pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.pinColor = MKPinAnnotationColorRed;
}
}
return pinView;
The problem of course is that I have to return pinView, which I allocate and need to release at some point. However, once I return pinView, I cannot release it because the method call terminates. How do I get around this? Thanks!
Using autorelease
is a way to solve issues like this~ Although, be aware of what autorelease pool your object is being placed into to ensure proper lifespan. =)
Very valuable documentation link for anyone figuring this stuff out: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html
精彩评论