0

嘿专家,我在使用 NSThread 时遇到了一些麻烦。Xcode 不断给我“ * __NSAutoreleaseNoPool(): NSCFString 类的对象 0x5694dc0 自动释放,没有适当的池 - 只是泄漏”错误。

我使用 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 行正确地声明了池。

然后在我的循环结束时,我使用:[pool release];

是因为我使用委托方法作为 performSelectorInBackground 吗?感谢堆栈溢出。

    - (void)preFetch { //process filenames to be downloaded and assign types to each one
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSArray *regions = [NSArray arrayWithObjects: @"dr_national", @"ds_ir", @"conus_FL360", @"FL360_conus", @"dr_nw", @"dr_nc", @"dr_ne", @"dr_sw", @"dr_sc", @"dr_se", @"ds_ir_nw", @"ds_ir_nc", @"ds_ir_ne", @"ds_ir_sw", @"ds_ir_sc", @"ds_ir_se", nil];
    NSError* error;
    for (NSString *regionDir in regions) {
        NSLog(@"region now: %@", regionDir); foo = 0;
        NSString *regUrl = [NSString stringWithFormat:@"http://someUrl/%@/index.lst", regionDir ];
        NSString* text1 = [NSString stringWithContentsOfURL:[NSURL URLWithString:regUrl ] encoding:NSASCIIStringEncoding error:&error];
        NSArray *listItems = [text1 componentsSeparatedByString:@"\n"];
        for (int k=0; k<[listItems count]; k++) {
            if ([[listItems objectAtIndex:k] length] != 0){
                NSString *newpath = [NSString stringWithFormat:@"http://someUrl/%@", [listItems objectAtIndex:k]];
                NSLog(@"newpath: %@",newpath);
                [self performSelectorInBackground:@selector(moveProgressBar) withObject:nil];
                [self fetchImages:newpath:type]; //pass multiple arguments to fetchImages, newpath and type
            }
        }
    }
    [pool release];
}

    - (void)moveProgressBar{
        [delegate increaseAmount];
    }
4

1 回答 1

1

您应该在您的方法中设置一个自动释放池,因为它是在不同的线程上调用的。

- (void)moveProgressBar
{
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     [delegate increaseAmount];
     [pool drain];
}

编辑

话虽如此,看看代码本身,您似乎正在尝试从后台线程更新 UI?任何这样做的代码都应该在主线程上执行。

如果您要运行一个长时间运行的进程,它不会锁定 UI,并让用户随时了解进度,典型的模式是在后台线程上自行进行处理,并使用performSelectorOnMainThread:.

于 2011-03-25T17:31:41.287 回答