views:

155

answers:

2

I create a second thread to call a method that downloads several images using:

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

It works fine but I get a long list of leaks in the log similar to:

2010-04-18 00:48:12.287 FS Companion[11074:650f] * _NSAutoreleaseNoPool(): Object 0xbec2640 of class NSCFString autoreleased with no pool in place - just leaking Stack: (0xa58af 0xdb452 0x5e973 0x5e770 0x11d029 0x517fa 0x51708 0x85f2 0x3047d 0x30004 0x99481fbd 0x99481e42)

2010-04-18 00:48:12.288 FS Companion[11074:650f] * _NSAutoreleaseNoPool(): Object 0xbe01510 of class NSCFString autoreleased with no pool in place - just leaking Stack: (0xa58af 0xdb452 0x5e7a6 0x11d029 0x517fa 0x51708 0x85f2 0x3047d 0x30004 0x99481fbd 0x99481e42)

2010-04-18 00:48:12.289 FS Companion[11074:650f] * _NSAutoreleaseNoPool(): Object 0xbde6720 of class NSCFString autoreleased with no pool in place - just leaking Stack: (0xa58af 0xdb452 0x5ea73 0x5e7c2 0x11d029 0x517fa 0x51708 0x85f2 0x3047d 0x30004 0x99481fbd 0x99481e42)

Can someone help me understand the problem?

+3  A: 

The error is "_NSAutoreleaseNoPool()". There is not an NSAutoreleasePool allocated by default in a thread. You need to create one yourself otherwise the -autorelease'd objects will be leaked.

Your -downloadImages therefore should look like this:

-(void)downloadImages {
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  ...
  [pool drain];
}
KennyTM
thank you so very much
Brodie
+1  A: 

I'm just on a similar issue... with funny nested threads leaking as hell.

Don't forget to release the pool too. :-)

-(void)downloadImages {
   NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
   ...
   [pool release];  
   pool =nil;
}
MacTouch
Ehhhmm... drain releases a pool already. I did not know that.
MacTouch