0

编辑:我在项目中使用 ARC

我像这样从 plist 加载注释:

[NSThread detachNewThreadSelector:@selector(loadPList) toTarget:self withObject:nil];

...

- (void) loadPList
{

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *path = [[documentPaths lastObject] stringByAppendingPathComponent:@"test.plist"];

    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; // memory leak here

    NSMutableArray *annotations = [[NSMutableArray alloc]init];


    dispatch_async(dispatch_get_main_queue(), ^{


        NSMutableArray * annotationsToRemove = [ mapView.annotations mutableCopy ] ;
        [ annotationsToRemove removeObject:mapView.userLocation ] ;
        [ mapView removeAnnotations:annotationsToRemove ] ;



        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"blackKey"])
        {

            NSArray *ann = [dict objectForKey:@"Black"];

            for(int i = 0; i < [ann count]; i++) {

                NSString *coordinates = [[ann objectAtIndex:i] objectForKey:@"Coordinates"];

                double realLatitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:1] doubleValue];
                double realLongitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:0] doubleValue];

                MyAnnotation *myAnnotation = [[MyAnnotation alloc] init];
                CLLocationCoordinate2D theCoordinate;
                theCoordinate.latitude = realLatitude;
                theCoordinate.longitude = realLongitude;

                myAnnotation.coordinate=CLLocationCoordinate2DMake(realLatitude,realLongitude);        
                myAnnotation.title = [[ann objectAtIndex:i] objectForKey:@"Name"];
                myAnnotation.subtitle = [[ann objectAtIndex:i] objectForKey:@"Address"];
                myAnnotation.icon = [[ann objectAtIndex:0] objectForKey:@"Icon"];

                [mapView addAnnotation:myAnnotation];
                [annotations addObject:myAnnotation];

            }

        }   


    });


}

一切正常,但内存泄漏工具向我显示了泄漏。

泄漏截图

4

3 回答 3

2

您需要将 the@autoreleasepool放在方法的开头 - 在它dictionaryWithContentsOfFile:之外调用该调用,您将创建一个没有池的自动释放对象,因此它会泄漏。根据线程编程指南

如果您的应用程序使用托管内存模型,那么创建自动释放池应该是您在线程进入例程中要做的第一件事。同样,销毁这个自动释放池应该是你在线程中做的最后一件事。

另外,我能问一下为什么你使用NSThread加载 plist 而不是dispatch_async()使用全局队列吗?我不经常看到dispatch_async()嵌套在线程分离中,所以只是好奇。

编辑:

要修复您的内存泄漏,而不干扰您的线程/GCD 混合,请像这样调用您的方法:

[NSThread detachNewThreadSelector:@selector(loadPList) toTarget:self withObject:nil];

并像这样实现它:

- (void) loadPList
{
    @autoreleasepool {
        NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *path = [[documentPaths lastObject] stringByAppendingPathComponent:@"test.plist"];

        NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; // memory leak here

        NSMutableArray *annotations = [[NSMutableArray alloc]init];


        dispatch_async(dispatch_get_main_queue(), ^{

            NSMutableArray * annotationsToRemove = [ mapView.annotations mutableCopy ] ;
            [ annotationsToRemove removeObject:mapView.userLocation ] ;
            [ mapView removeAnnotations:annotationsToRemove ] ;

            if ([[NSUserDefaults standardUserDefaults] boolForKey:@"blackKey"])
            {
                NSArray *ann = [dict objectForKey:@"Black"];

                for(int i = 0; i < [ann count]; i++) 
                {
                    NSString *coordinates = [[ann objectAtIndex:i] objectForKey:@"Coordinates"];

                    double realLatitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:1] doubleValue];
                    double realLongitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:0] doubleValue];

                    MyAnnotation *myAnnotation = [[MyAnnotation alloc] init];
                    CLLocationCoordinate2D theCoordinate;
                    theCoordinate.latitude = realLatitude;
                    theCoordinate.longitude = realLongitude;

                    myAnnotation.coordinate=CLLocationCoordinate2DMake(realLatitude,realLongitude);        
                    myAnnotation.title = [[ann objectAtIndex:i] objectForKey:@"Name"];
                    myAnnotation.subtitle = [[ann objectAtIndex:i] objectForKey:@"Address"];
                    myAnnotation.icon = [[ann objectAtIndex:0] objectForKey:@"Icon"];

                    [mapView addAnnotation:myAnnotation];
                    [annotations addObject:myAnnotation];
                }
            }   
        }
        );
    }
}
于 2012-09-29T17:23:00.490 回答
0

如果不出意外,您需要一个自动释放池。从文档中引用detachNewThreadSelector,“该方法aSelector负责为新分离的线程设置一个自动释放池,并在它退出之前释放该池。”

就个人而言,我可能只是loadPlist通过 GCD 而不是调用detachNewThreadSelector,然后您就不必担心自动释放池:

dispatch_async(get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [self loadPlist];
});
于 2012-09-28T13:52:15.720 回答
-1
NSMutableArray *annotations = [[NSMutableArray alloc]init]; // Never released
NSMutableArray * annotationsToRemove = [ mapView.annotations mutableCopy ] ; // Never released
MyAnnotation *myAnnotation = [[MyAnnotation alloc] init]; // Never released

您的方法应如下所示:

- (void) loadPList {
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *path = [[documentPaths lastObject] stringByAppendingPathComponent:@"test.plist"];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; // memory leak here
    NSMutableArray *annotations = [[[NSMutableArray alloc] init] autorelease];
    dispatch_async(dispatch_get_main_queue(), ^{
        NSMutableArray * annotationsToRemove = [[mapView.annotations mutableCopy] autorelease];
        [annotationsToRemove removeObject:mapView.userLocation] ;
        [mapView removeAnnotations:annotationsToRemove] ;
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"blackKey"])
        {

            NSArray *ann = [dict objectForKey:@"Black"];

            for(int i = 0; i < [ann count]; i++) {

                NSString *coordinates = [[ann objectAtIndex:i] objectForKey:@"Coordinates"];

                double realLatitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:1] doubleValue];
                double realLongitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:0] doubleValue];

                MyAnnotation *myAnnotation = [[[MyAnnotation alloc] init] autorelease];
                CLLocationCoordinate2D theCoordinate;
                theCoordinate.latitude = realLatitude;
                theCoordinate.longitude = realLongitude;

                myAnnotation.coordinate=CLLocationCoordinate2DMake(realLatitude,realLongitude);
                myAnnotation.title = [[ann objectAtIndex:i] objectForKey:@"Name"];
                myAnnotation.subtitle = [[ann objectAtIndex:i] objectForKey:@"Address"];
                myAnnotation.icon = [[ann objectAtIndex:0] objectForKey:@"Icon"];

                [mapView addAnnotation:myAnnotation];
                [annotations addObject:myAnnotation];

            }
        }   
    });
}
于 2012-09-28T12:32:31.803 回答