PerformSelectorInBackground在设备上泄漏

时间:2010-05-24 01:07:40

标签: iphone objective-c cocoa multithreading memory

虽然它似乎不会在模拟器上造成问题,但在设备上使用performSelectorInBackground会导致内存泄漏。或者至少那是仪器所指出的。看看代码我没有一个线索可能是什么原因。 我试图将受影响的代码剥离到最低限度但仍然很奇怪每次执行此代码时,Instruments都会显示泄漏。

这里有什么异常吗?

 //In viewcontrollerA:
    -(void)mainLoop
    {
     [self.viewControllerB performSelectorInBackground:@selector(calculateTotals) withObject:nil];

            //This gives the same problem
         //[NSThread detachNewThreadSelector:@selector(calculateTotals) toTarget:self.viewControllerB withObject:nil];

            //UI stuff ...

    }

    //viewControllerB:
    -(void)calculateTotals
    {
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

     //Heavy calculations ...


     [pool release];
    }

修改 我仍然在研究这个问题,似乎泄漏是由于堆栈中的某个地方[NSThread start]从未跟随[NSThread退出]。所以看起来偶尔会有一个线程在没有结束的情况下继续运行。 现在我的问题是,我能做些什么来手动结束那些“悬挂”威胁?

1 个答案:

答案 0 :(得分:0)

也许您的一个主题是抛出异常?线程中的异常不会在调试控制台中报告,您必须在线程中捕获异常。