与iPhone SDK一起使用for(数组中的对象)时内存泄漏

时间:2009-11-18 19:56:42

标签: objective-c iphone memory-leaks for-loop instruments

我正在构建我的一个应用程序中遇到一些严重的内存泄漏。我有一个UITavBarview内的UINavigatonController。 NavView内部是MKMap视图。单击标注上的附件按钮时,将加载详细视图。在该详细视图中,我试图使用for(数组中的对象)循环从plist填充表。 plist是一系列字典。我正在运行字典,找到一个带有标题的键,然后从该字典中获取一个数组。这一切在模拟器中工作正常,但我正在以我的方式做大量的内存泄漏。任何想法发生了什么?

     - (void)viewDidLoad {
        self.title = @"Route Details";
        NSString *path = [[NSBundle mainBundle] pathForResource:@"stopLocation" ofType:@"plist"];
        holderArray  = [[NSMutableArray alloc] initWithContentsOfFile:path];
        [self getRouteArray];
        routeDetails.delegate = self;
        routeDetails.dataSource = self;

    }
    -(void)getRouteArray{

        for (NSMutableDictionary *dictionary in holderArray) {
            //NSString *stopName = [dictionary objectForKey:@"StopName"];
            //NSString *stopName = [[NSString alloc] initWithString:[dictionary objectForKey:@"StopName"]];

            BOOL testString = [currentRoute isEqualToString:[dictionary objectForKey:@"StopName"]];

            if (testString) {
                routeArray = [[NSMutableArray alloc] initWithArray:[dictionary objectForKey:@"RouteService"]];
            }
        }
    }
- (void)dealloc {

    [routeArray release];
    [routeDetails release];

    [super dealloc];
}

holderArray是一个ivar,路由数组也是如此。正如您所看到的,我已经尝试了几种分配nstrings和数组的方法,但似乎都会产生相同的泄漏。根据我从NSCFString,NSCFDictionary和NSCFArry泄漏的性能工具。我在dealloc中发布了routeArray并且工作正常,但是如果我发布holderArray,每当我从详细视图返回到我的地图时它都会崩溃。我想我真的不确定如何处理for循环中使用的字符串和字典。

只是为了添加详细视图,就像这样创建:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{


    NSString *selectedRouteName = [NSString stringWithFormat:@"%@",view.annotation.title];

    RouteDetailView *rdc = [[RouteDetailView alloc] initWithNibName:@"RouteDetailView" bundle:nil];
    [rdc setCurrentRoute:selectedRouteName];
    [self.navigationController pushViewController:rdc animated:YES];
    [rdc release];


}

对不起,如果上述任何一项不清楚。让我知道,我可以试着改写它。

1 个答案:

答案 0 :(得分:3)

testString中最多只有一个密钥holderArray是真的吗?如果是这样,您应该在设置routeArray后突破循环。如果没有,那么您可能会多次设置routeArray,除了您分配给它的最后一个数组之外的所有数组都将被泄露。

另外,我没有看到你发布holderArray