我在设备上的仪器中运行泄漏工具。我正在泄漏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
答案 0 :(得分:3)
你肯定在这里泄露记忆:
self.loc = [[MKPlacemark alloc] initWithCoordinate:locCoord
addressDictionary:locDict];
由于您已将loc
定义为retain
属性,因此分配给它将在参数上调用retaing
,您最终会得到MKPlacemark
该商品保留,永不释放。
我会按如下方式更改该行:
self.loc = [[[MKPlacemark alloc] initWithCoordinate:locCoord
addressDictionary:locDict] autorelease];
另外,请仔细检查此模式的代码,因为正如@omz所建议的那样,在分配给reverseGeocoder
时也是如此。
答案 1 :(得分:2)
在viewDidLoad
方法中,您可以设置reverseGeocoder
属性,如下所示:
self.reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:locCoord];
这会泄漏,因为属性设置器已经保留了对象。您需要在设置属性后释放或自动释放MKReverseGeocoder
实例,例如:
self.reverseGeocoder = [[[MKReverseGeocoder alloc] initWithCoordinate:locCoord] autorelease];