【问题标题】:iphone - memory leaks in separate threadiphone - 单独线程中的内存泄漏
【发布时间】:2010-04-18 06:54:55
【问题描述】:

我创建了第二个线程来调用一个下载多个图像的方法:

[NSThread detachNewThreadSelector:@selector(downloadImages) toTarget:self withObject:nil];

它工作正常,但我在日志中得到一长串泄漏,类似于:

2010-04-18 00:48:12.287 FS Companion[11074:650f] *** _NSAutoreleaseNoPool(): NSCFString 类的对象 0xbec2640 自动释放,没有适当的池 - 只是泄漏 堆栈:(0xa58af 0xdb452 0x5e973 0x5e770 0x11d029 0x517fa 0x51708 0x85f2 0x3047d 0x30004 0x99481fbd 0x99481e42)

2010-04-18 00:48:12.288 FS Companion[11074:650f] *** _NSAutoreleaseNoPool(): NSCFString 类的对象 0xbe01510 自动释放,没有适当的池 - 只是泄漏 堆栈:(0xa58af 0xdb452 0x5e7a6 0x11d029 0x517fa 0x51708 0x85f2 0x3047d 0x30004 0x99481fbd 0x99481e42)

2010-04-18 00:48:12.289 FS Companion[11074:650f] *** _NSAutoreleaseNoPool(): NSCFString 类的对象 0xbde6720 自动释放,没有适当的池 - 只是泄漏 堆栈:(0xa58af 0xdb452 0x5ea73 0x5e7c2 0x11d029 0x517fa 0x51708 0x85f2 0x3047d 0x30004 0x99481fbd 0x99481e42)

有人可以帮我理解这个问题吗?

【问题讨论】:

    标签: iphone objective-c multithreading memory-leaks


    【解决方案1】:

    错误是“_NSAutoreleaseNoPool()”。线程中默认没有分配 NSAutoreleasePool。您需要自己创建一个,否则-autorelease'd 对象将被泄露。

    因此,您的 -downloadImages 应如下所示:

    -(void)downloadImages {
      NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
      ...
      [pool drain];
    }
    

    【讨论】:

      【解决方案2】:

      我只是在一个类似的问题上......有趣的嵌套线程泄漏得像地狱一样。

      不要忘记也释放池。 :-)

      -(void)downloadImages {
         NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
         ...
         [pool release];  
         pool =nil;
      }
      

      【讨论】:

      • Ehhhmm... drain 已经释放了一个池。我不知道。
      猜你喜欢
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-29
      • 2013-12-18
      • 2010-11-23
      • 2011-12-10
      • 2011-05-24
      相关资源
      最近更新 更多