在后台线程中调用函数时如何避免内存泄漏

时间:2009-12-26 10:07:51

标签: objective-c

我在后台线程中调用函数时遇到内存泄漏问题,如

[self performSelectorInBackground:@ selector(myFunctionName)withObject:nil];

在后台调用此函数时会出现以下警告 显示:

2009-12-26 16:05:46.777 SimpleGame[11450:99fb] *** _NSAutoreleaseNoPool(): Object 0x63851d0 of class NSURL autoreleased with no pool in place - just leaking Stack: (0x305a2e6f 0x30504682 0x305440c1 0x8ca69 0x8cbe0 0x3050a79d 0x3050a338 0x923c4155 0x923c4012)
2009-12-26 16:05:46.800 SimpleGame[11450:99fb] *** _NSAutoreleaseNoPool(): Object 0xe81090 of class NSURLRequest autoreleased with no pool in place - just leaking Stack: (0x305a2e6f 0x30504682 0x8ca9b 0x8cbe0 0x3050a79d 0x3050a338 0x923c4155 0x923c4012)
2009-12-26 16:05:46.816 SimpleGame[11450:99fb] *** _NSAutoreleaseNoPool(): Object 0x63cc720 of class NSCFString autoreleased with no pool in place - just leaking Stack: (0x305a2e6f 0x30504682 0x305fed64 0x3058ae69 0x8cadb 0x8cbe0 0x3050a79d 0x3050a338 0x923c4155 0x923c4012)
2009-12-26 16:05:46.819 SimpleGame[11450:99fb] *** _NSAutoreleaseNoPool(): Object 0xe0b480 of class NSCFString autoreleased with no pool in place - just leaking Stack: (0x305a2e6f 0x30504682 0x30603183 0x305fed80 0x3058ae69 0x8cadb 0x8cbe0 0x3050a79d 0x3050a338 0x923c4155 0x923c4012)
2009-12-26 16:05:46.840 SimpleGame[11450:99fb] *** _NSAutoreleaseNoPool(): Object 0x63a2dc0 of class NSCFString autoreleased with no pool in place - just leaking Stack: (0x305a2e6f 0x30504682 0x305fedb6 0x3058ae69 0x8cadb 0x8cbe0 0x3050a79d 0x3050a338 0x923c4155 0x923c4012)
2009-12-26 16:05:46.846 SimpleGame[11450:99fb] *** _NSAutoreleaseNoPool(): Object 0x64060b0 of class NSCFString autoreleased with no pool in place - just leaking Stack: (0x305a2e6f 0x30504682 0x30560183 0x305fedd2 0x3058ae69 0x8cadb 0x8cbe0 0x3050a79d 0x3050a338 0x923c4155 0x923c4012)
2009-12-26 16:05:46.880 SimpleGame[11450:99fb] *** _NSAutoreleaseNoPool(): Object 0x63b25c0 of class NSThread autoreleased with no pool in place - just leaking Stack: (0x305a2e6f 0x30504682 0x30509c04 0x3055fe8e 0x923b589e 0x30560f25 0x30570c41 0x30570ae3 0x30570a67 0x923b589e 0x30570a15 0x305fee2c 0x3058ae69 0x8cadb 0x8cbe0 0x3050a79d 0x3050a338 0x923c4155 0x923c4012)
2009-12-26 16:05:46.911 SimpleGame[11450:99fb] *** _NSAutoreleaseNoPool(): Object 0x6329660 of class NSCFArray autoreleased with no pool in place - just leaking Stack: (0x305a2e6f 0x30504682 0x3055fee0 0x923b589e 0x30560f25 0x30570c41 0x30570ae3 0x30570a67 0x923b589e 0x30570a15 0x305fee2c 0x3058ae69 0x8cadb 0x8cbe0 0x3050a79d 0x3050a338 0x923c4155 0x923c4012)

我不知道该怎么办。所以有人帮助我。

2 个答案:

答案 0 :(得分:2)

每个线程都应该为它创建NSAutoreleaseNoPool。

答案 1 :(得分:1)

Girish是对的,每个线程都需要它自己的自动释放池。这是一个例子:

- (void)myBackgroundMethod
{
  NSAutoreleasePool*pool=[[NSAutoreleasePool alloc] init];

  /* Code Body */

  [pool release];
  return;
}