Sheehan Alam
Sheehan Alam

Reputation: 60869

EXC_BAD_ACCESS with NSAutoReleasePool

I am detaching a new thread

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

I am getting an EXC_BAD_ACCESS on

STObject* st = [cellitem get:@"stobject"];

In my following method

-(void)loadAvatar
{   

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    STObject* st = [cellitem get:@"stobject"];
    //do stuff...
    [pool release];
}

I have tried retaining st but no luck. When I run this code without detaching a new thread I have no problems. I am not really sure what I am missing.

UPDATE cellitem is a subclass of NSObject that contains some properties such as a dictionary and strings.

The get method basically returns a string from a dictionary

Upvotes: 0

Views: 1870

Answers (2)

Sheehan Alam
Sheehan Alam

Reputation: 60869

Not exactly sure why this solution works, but I told my thread to sleep for 0.1 seconds and it seems to solve all of the problems.

    [NSThread detachNewThreadSelector:@selector(loadAvatar) toTarget:self withObject:nil];
    [NSThread sleepForTimeInterval:0.1];

Does anyone know why this is so? I am guessing sleeping prevents some object from being released too early? Could be a hacky fix.

Upvotes: 1

glorifiedHacker
glorifiedHacker

Reputation: 6420

Retaining st is not going to do you much good, since the EXC_BAD_ACCESS error originates from before the assignment takes place. The problem lies either with accessing cellItem or in your get method. Either way you are likely trying to access an object that has already been released. Try running Instruments with zombie detection enabled.

Upvotes: 3

Related Questions