阻止和保留周期不能赶上它

我有一个块和弱引用的问题,我在ARC下。 我build立了一个类,它是一个免费的项目,是一个围绕Google Directions API的简单包装,你可以在这里下载: link to the project
我在视图控制器内使用它的问题是,使用它后,视图控制器不会被释放。 我想这是这个对象的问题,因为如果我注释掉或设置为零,一切正常。 我无法理解保留周期在哪里,当然我设置为弱自我,这里是我使用它的视图控制器的方法:

- (void) getDirections{ __weak RouteMapViewController * weakSelf = self; self.routeObject = [[RouteDirectionsObject alloc]init]; [self.mapView removeAnnotations:self.mapView.annotations]; [self.mapView removeOverlays:self.mapView.overlays]; [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; [_routeObject createDirectionRequestWithStartPoint:weakSelf.startPoint andEndPoint:weakSelf.endPoint withCallBackBlock:^(NSError *error, NSDictionary *routeDistance, NSDictionary *routeDuration, MKPolyline *routePolyline, NSArray *routes, NSArray *steps, CLLocation *startPoint, CLLocation *endPoint, NSArray *directions) { [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; Annotation * startAnnotation = [[Annotation alloc]initWithCoordinate:startPoint.coordinate title:NSLocalizedString(@"YOUR_POSITION_KEY", @"Your position") annotationType:AnnotationTypeStart]; Annotation * endAnnotation = [[Annotation alloc]initWithCoordinate:endPoint.coordinate title:NSLocalizedString(@"AIRPORT_POSITION_KEY", @"Airport position") annotationType:AnnotationTypeEnd]; NSArray * annotationArray = [NSArray arrayWithObjects:startAnnotation, endAnnotation, nil]; weakSelf.routeSteps = steps; weakSelf.routeDirections = directions; weakSelf.duration = routeDuration; weakSelf.distance = routeDistance; CLLocationDegrees maxLat = -90.0f; CLLocationDegrees maxLon = -180.0f; CLLocationDegrees minLat = 90.0f; CLLocationDegrees minLon = 180.0f; for (int i = 0; i < weakSelf.routeSteps.count; i++) { NSDictionary * stepDictCoordinate = [[weakSelf.routeSteps objectAtIndex: i]objectForKey:@"start_location"]; CLLocationCoordinate2D currentLocationCoordinate = CLLocationCoordinate2DMake([[stepDictCoordinate objectForKey:@"lat"]doubleValue], [[stepDictCoordinate objectForKey:@"lng"]doubleValue]); if(currentLocationCoordinate.latitude > maxLat) { maxLat = currentLocationCoordinate.latitude; } if(currentLocationCoordinate.latitude < minLat) { minLat = currentLocationCoordinate.latitude; } if(currentLocationCoordinate.longitude > maxLon) { maxLon = currentLocationCoordinate.longitude; } if(currentLocationCoordinate.longitude < minLon) { minLon = currentLocationCoordinate.longitude; } } MKCoordinateRegion region; region.center.latitude = (maxLat + minLat) / 2; region.center.longitude = (maxLon + minLon) / 2; region.span.latitudeDelta = maxLat - minLat; region.span.longitudeDelta = maxLon - minLon; dispatch_async(dispatch_get_main_queue(), ^{ if ( error) { UIAlertView * alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Error", @"Error alert view title") message:NSLocalizedString(@"KEY_DIRECTIONS_ERROR", @"Alert error message for directions") delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; [_routesButton setEnabled:NO]; } else{ [weakSelf.mapView addAnnotations:annotationArray]; [_routesButton setEnabled:YES]; if(routePolyline){ [weakSelf.mapView addOverlay:routePolyline]; } else{ UIAlertView * alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Error", @"Error alert view title") message:NSLocalizedString(@"KEY_DIRECTIONS_POLYLINE_ERROR", @"Polyline inconsistant") delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; } //[weakSelf.mapView setRegion:region animated:YES]; [weakSelf setRegion:region]; } }); }];} 

如果我把断点,并要求视图控制器的retainCount,我可以看到,即使通过视图控制器设置为弱,不同的时间递增。 任何帮助将非常感激。
谢谢,
安德里亚

/ * ** * ** * ** * ** 更新 * ** * ** * ** * * / **
检查分配,我可以看到,在块内的视图控制器被保留了很多次传递一个名为-tryRetain的方法,直到递减,但似乎错过了释放释放一个。 为了清晰起见,我必须指定传递的块被复制到类path方向对象中。 我做了一个小样本,你可以在这里下载 : 下载项目

你不应该使用自己的类的实例variables(块),在你的情况下,_routerButton

对象的绝对保留数量是毫无意义的; http://www.whentouseretaincount.com(底部有一些链接描述技术细节)&#x3002;

泄漏仪器不太可能帮助。 这可能,但也许不是。 分配工具,但是,你的朋友。 打开“logging引用计数”和“仅跟踪活动分配”。 在乐器中运行您的应用程序,然后查找应该消失的对象,但不是。 点击任何一个将显示该对象的保留/释放事件,这将回答额外保留来自哪里的问题。

更可能的是,鉴于这是一个视图对象,这是因为它仍然在视图层次结构中,而是埋在其他不透明的视图之后。 它也可以作为一个计时器的目标或caching,可能是一个“回”风格的导航caching。

你可以使用仪器来确定什么是保留和释放你的对象,并找出不平衡发生的地方。 这可以通过在构build和运行项目时selectconfiguration文件来完成,然后启动仪器。

一旦进入,你可以select泄漏来确定你的泄漏发生的地方。 在这里输入图像说明

您可以使用一些指南来掌握仪器: