当我执行选择器InBackground: 时,为什么没有自动释放池?

Why is there no autorelease pool when I do performSelectorInBackground:?(当我执行选择器InBackground: 时,为什么没有自动释放池?)

本文介绍了当我执行选择器InBackground: 时,为什么没有自动释放池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用一个进入后台线程的方法:

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:

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)

如果我取消自动释放池,我会收到一大堆这样的消息.我还尝试围绕 performSelectorInBackground: 的调用创建一个自动释放池,但这无济于事.

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.

我怀疑这个参数,但我不知道为什么编译器会抱怨 NSCFNumber.我错过了什么吗?

I suspect the parameter, but I don't know why the compiler complains about an NSCFNumber. Am I missing something?

我的实例变量都是非原子的".这会是个问题吗?

My Instance variables are all "nonatomic". Can that be a problem?

更新:我可能还怀疑某些变量已添加到主线程的自动释放池中(可能是 ivar),现在它试图在错误的自动释放池中释放那个变量?如果是这样,我该如何解决?(该死,这个线程的东西很复杂;))

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? If so, how could I fix that? (damn, this threading stuff is complex ;) )

推荐答案

这很可能是因为泄漏的对象(一个 NSNumber)是从线程外部传入的参数.因此,该变量属于调用线程(及其自动释放池)

Most likely the reason for this is because the leaked object (an NSNumber), is a parameter passed in from outside the thread. Hence, this variable belongs to the calling thread (and its autorelease pool)

线程调用周围的自动释放池不起作用的原因是线程创建者 (performSelectorInbackground) - 立即返回,很可能在线程仍在运行时返回.

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.

我建议您在将选择器的参数作为参数传入后对其进行释放.

I suggest you do a release on your selector's parameter after passing it in as an argument.

这篇关于当我执行选择器InBackground: 时,为什么没有自动释放池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:当我执行选择器InBackground: 时,为什么没有自动释放池?

基础教程推荐