views:

178

answers:

2

I do this:

NSString *fullpath = [[NSBundle mainBundle] pathForResource:@"text_file" ofType:@"txt"];

Why the following message appear? Is my code leaking?

2010-03-31 13:44:18.649 MJIPhone[2175:207] *** _NSAutoreleaseNoPool(): Object 0x3909ba0 of class NSPathStore2 autoreleased with no pool in place - just leaking
Stack: (0x1656bf 0xc80d0 0xcf2ad 0xcee0e 0xd3327 0x2482 0x2426)
2010-03-31 13:44:18.653 MJIPhone[2175:207] *** _NSAutoreleaseNoPool(): Object 0x390b0b0 of class NSPathStore2 autoreleased with no pool in place - just leaking
Stack: (0x1656bf 0xc80d0 0xc7159 0xd0c6f 0xd3421 0x2482 0x2426)
2010-03-31 13:44:18.672 MJIPhone[2175:207] *** _NSAutoreleaseNoPool(): Object 0x390d140 of class NSCFString autoreleased with no pool in place - just leaking
Stack: (0x1656bf 0xc6e62 0xcec1b 0xd4386 0x24ac 0x2426)
+1  A: 

At the time you run that line of code, no NSAutoreleasePool has been created on the current thread.

If you're running on the main thread, Cocoa (and Cocoa Touch) automatically provides an autorelease pool for you. If you've scheduled something to happen on a separate thread (which would also include something scheduled by performSelectorInBackground:withObject:), then you need to provide your own autorelease pool.

BJ Homer
+2  A: 

This happens because you are running in a thread. User threads don't share the main threads autorelease pool, so you need to create your own. Otherwise, objects like these will never get released, thus leaking.

In the beginning of your thread method, before the loop or whatever, do:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

Before returning, release it:

[pool drain];
calmh
Shouldn't that be `[pool drain]`?
Frank Schmitt
Hmm, looks like they're synonymous on non-GC systems.
Frank Schmitt
@Frank Interesting. I always though `release` was more idiomatic, since anything else somehow implied that the pool would be left alive after the call. Besides, `drain` reads like a bad pun. But as you say, they're synonymous and the documentation actually states that "You should typically use drain instead of release.".
calmh