I stumbled into a problem when I hit the “build and analyze” button on the Build menu in my Xcode. The analysis suggest me to release a variable that I wish later on to be returned. The code is like following:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
//I do some other thing here
MKPinAnnotationView *annView=
[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"addressLocation"];
//I do some other thing here
return annView;
}
I can I release annView and return it without causing any problem?
This is precisely what autorelease is intended for. That method should autorelease it.
I’d suggest reading the memory management guide if you’re unclear about this sort of thing. It’s pretty short and explains all this stuff very well. Once you understand that guide, you won’t ever have to wonder again.