views:

148

answers:

2

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

+2  A: 

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.

glorifiedHacker
A: 

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.

Sheehan Alam
I would be careful in settling on a fix that you don't fully understand. There is a good chance that you haven't really fixed the problem, but rather just temporarily masked it. Did you try enabling zombie detection to determine which object you were messaging after release?
glorifiedHacker
zombie detection was showing cellitem, but I traced the code and it is being released properly (or so I think). Where would you go from here?
Sheehan Alam
You’ve got to simplify the code until you understand what’s going on or until you can post the details here so that we don’t have to guess.
zoul
When using zombie detection in Instruments, you need to trace back through all of the releases happening on cellitem. If cellitem is showing up as a zombie, it definitely means that you are messaging it after it would have been deallocated.
glorifiedHacker