简体   繁体   English

为什么在执行performSelectorInBackground时没有自动释放池:?

[英]Why is there no autorelease pool when I do performSelectorInBackground:?

I am calling a method that goes in a background thread: 我正在调用后台线程中的方法:

[self performSelectorInBackground:@selector(loadViewControllerWithIndex:) withObject:[NSNumber numberWithInt:viewControllerIndex]];

then, I have this method implementation that gets called by the selector: 然后,我有这个方法实现,由选择器调用:

- (void) loadViewControllerWithIndex:(NSNumber *)indexNumberObj {
    NSAutoreleasePool *arPool = [[NSAutoreleasePool alloc] init];
    NSInteger vcIndex = [indexNumberObj intValue];

    Class c;
    UIViewController *controller = [viewControllers objectAtIndex:vcIndex];

    switch (vcIndex) {
        case 0:
            c = [MyFirstViewController class];
            break;
        case 1:
            c = [MySecondViewController class];
            break;
        default:
            NSLog(@"unknown index for loading view controller: %d", vcIndex); // error
            break;
    }

    if ((NSNull *)controller == [NSNull null]) {
        controller = [[c alloc] initWithNib];
        [viewControllers replaceObjectAtIndex:vcIndex withObject:controller];
        [controller release];
    }

    if (controller.view.superview == nil) {
        UIView *placeholderView = [viewControllerPlaceholderViews objectAtIndex:vcIndex];
        [placeholderView addSubview:controller.view];
    }

    [arPool release];
}

Althoug I do create an autorelease pool there for that thread, I always get this error: Althoug我确实为那个线程创建了一个自动释放池,我总是得到这个错误:

2009-05-30 12:03:09.910 Demo[1827:3f03] *** _NSAutoreleaseNoPool(): Object 0x523e50 of class NSCFNumber autoreleased with no pool in place - just leaking
Stack: (0x95c83f0f 0x95b90442 0x28d3 0x2d42 0x95b96e0d 0x95b969b4 0x93a00155 0x93a00012)

If I take away the autorelease pool, I get a whole bunch of messages like these. 如果我拿走自动释放池,我会得到一大堆这样的消息。 I also tried to create an autorelease pool around the call of the performSelectorInBackground:, but that doesn't help. 我还试图围绕performSelectorInBackground:的调用创建一个自动释放池,但这没有帮助。

I suspect the parameter, but I don't know why the compiler complains about an NSCFNumber. 我怀疑参数,但我不知道为什么编译器抱怨NSCFNumber。 Am I missing something? 我错过了什么吗?

My Instance variables are all "nonatomic". 我的实例变量都是“非原子的”。 Can that be a problem? 这可能是个问题吗?

UPDATE: I may also suspect that some variable has been added to an autorelease pool of the main thread (maybe an ivar), and now it trys to release that one inside the wrong autorelease pool? 更新:我可能还怀疑某个变量已被添加到主线程的自动释放池(可能是一个ivar),现在它试图在错误的自动释放池中释放那个? If so, how could I fix that? 如果是这样,我该如何解决? (damn, this threading stuff is complex ;) ) (该死的,这个穿线的东西很复杂;))

Most likely the reason for this is because the leaked object (an NSNumber), is a parameter passed in from outside the thread. 最有可能的原因是因为泄漏的对象(NSNumber)是从线程外部传入的参数。 Hence, this variable belongs to the calling thread (and its autorelease pool) 因此,此变量属于调用线程(及其自动释放池)

The reason that the autorelease pool around the thread call doesn't work, is because the thread creator (performSelectorInbackground) - returns immediately, most likely while the thread is still running. 线程调用周围的自动释放池不起作用的原因是因为线程创建者(performSelectorInbackground) - 立即返回,很可能是在线程仍在运行时。

I suggest you do a release on your selector's parameter after passing it in as an argument. 我建议你在将参数作为参数传递之后对你的选择器参数进行一次发布。

I agree that most likely the reason for this is because the leaked object (an NSNumber), is a parameter passed in from outside the thread. 我同意这很可能是因为泄漏的对象(NSNumber)是从线程外部传入的参数。 Hence, this variable belongs to the calling thread (and its autorelease pool) 因此,此变量属于调用线程(及其自动释放池)

The calling thread should use NSAutoreleasePool and I suggest that you add a retain instruction to your parameter as: 调用线程应该使用NSAutoreleasePool ,我建议你为参数添加一个retain指令:

- (void) loadViewControllerWithIndex:(NSNumber *)indexNumberObj {
    NSAutoreleasePool *arPool = [[NSAutoreleasePool alloc] init];
    [indexNumberObj retain];

    ....

    [arPool release];
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM