views:

65

answers:

3

Hi guys,

i'm having a bad time with some little code running on the iphone

basically, i just press a button, it calls runTest, it runs test method on a background thread. That's why I created an autorelease pool.

if i run the below code i got a beautiful message on the console saying:
2010-09-07 11:45:15.527 test[1312:207] *** -[CFString release]: message sent to deallocated instance 0x3d52ba0

-(void) test {
    NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init];

    NSString *xml = [[NSString alloc] initWithFormat:@"<%@>", @"msg"];
    NSLog(@"%@\n",xml);
    [xml release];

    [apool release]; // <-- this line throws the error msg  
}

- (IBAction) runTest: (id)sender
{
    [self performSelectorInBackground:@selector(test) withObject:nil];

}

i have found that: if i do not run test on a background thread (no autorelease pool), just calling [self test], the code works fine.

so, i think the problem is around the thread + autorelease pool, what am I doing wrong? and how may i solve it?

thanks!!

pd. i have the NSZombie flag enabled

A: 

NSLog may have to send a message to the UI thread (perform selector on main thread) to print serialized to the console, by which time, your xml is already released.

hotpaw2
interesting, I deleted: `NSLog(@"%@\n",xml);` and the problem remains`
jhon
Good guess, but wrong -- NSLog does its thing on whatever thread it is called on.
bbum
A: 

It shouldn't matter in this case, but you should always drain pools and never release them.

That is a truly bizarre error, if that is all the code.

The first thing I'd suggest is running the code with zombie detection enabled while recording all retain/release events in Instruments. That'll give you an inventory of retain/releases on the offending object. Post the results if it still doesn't make sense.

bbum
A: 

Let me see if I understand your question....

If you created an autorelease pool why you do the manual release with [apool release] ???

Remove that line and see what happens running the code in the background.

Good luck!

Julio
The -release is valid, though -drain is the right way. When manually implementing threads, you have to manage the pools manually, too.
bbum