将 for(object in array) 与 iPhone SDK 一起使用时出现内存泄漏

发布于 2024-08-11 14:42:20 字数 2085 浏览 5 评论 0原文

我正在构建的一个应用程序中遇到了一些严重的内存泄漏。我有一个 UINavigatonController 位于 UITabBarview 内。 NavView 内部是一个 MKMap 视图。当您单击标注上的附件按钮时,会加载详细视图。在该详细视图中,我尝试使用 for(array 中的对象) 循环从 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,route 数组也是一个。正如您所看到的,我尝试了几种分配 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];


}

抱歉,如果以上任何内容不清楚。让我知道,我可以尝试重新表述。

I am running into some serious memory leaks in one of my applications I am building. I have a UINavigatonController that is inside a UITabBarview. Inside the NavView is a MKMap view. When you click an accessory button on a callout a detail view is loaded. In that detail view I am trying to populate a table from a plist using a for(object in array) loop. The plist is an array of dictionaries. I am running though the dictionaries to find one with a key that is the title of the callout and then get an array from inside that dictionary. It all works fine in the simulaor but I am getting massive memory leaks doing it the way I am. Any Idea whats going on?

     - (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 is an ivar and so is route array. As you can see I have tried a few ways of allocating the nstrings and arrays but all seem to yield the same leaks. According to the performance tool I am leaking from NSCFString, NSCFDictionary, and the NSCFArry. I released the routeArray in the dealloc and it works fine, but if I release holderArray it crashes whenever I go back to my map from the detail view. I guess I am just really unsure as to how to deal with the strings and dictionary used in the for loop.

Just to add the detail view is being created like so:

- (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];


}

Sorry if any of the above is unclear. Let me know and I can try to rephrase it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瘫痪情歌 2024-08-18 14:42:20

对于 holderArray 中的最多一个键,testString 是否为 true?如果是这样,您可能应该在设置routeArray后跳出循环。如果没有,那么您可能会多次设置routeArray,并且除了最后分配给它的数组之外的所有数组都将被泄漏。

另外,我没有看到您释放 holderArray

Will testString be true for at most one key in holderArray? If so, you should probably break out of the loop after setting routeArray. If not, then you may be setting routeArray multiple times, and all but the last array you assigned to it would be leaked.

Also, I don't see you releasing holderArray.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文