PDA

View Full Version : Crashed by using NSThread, help me, thanks




xingworld
Jan 24, 2011, 09:04 PM
I dev for iphone, My code is:
----------------------------------------------------------------------------------------------------

- (void)submitAnswersAndGetResult
{
[ NSThread detachNewThreadSelector:@selector(_ThreadSubmitAnswersAndGetResult) toTarget:self withObject:nil ];
}

- (void)_ThreadSubmitAnswersAndGetResult
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

@try
{
// ...
// ...
[self performSelectorOnMainThread:@selector(_MainThreadShowResult) withObject:nil waitUntilDone:YES];
}
@catch (NSException * e)
{
}


// If removes the following line, program will not crash
[pool release];
}


- (void)_MainThreadShowResult
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

// ...
// ...
// ...

[pool release];
}


call submitAnswersAndGetResult for the first time, it works well, but the second time, it crashed.

I read the XCode document, and find the following descriptions:
----------------------------------------------------------------------------------------------------
For non garbage-collected applications, the method aSelector is responsible for setting up an autorelease pool for the newly detached thread and freeing that pool before it exits. Garbage-collected applications do not need to create an autorelease pool.

But, how I knew that my program is a non garbage-collected applications or a Garbage-collected applications?

Do I need to create an autorelease pool?

Thank you very much!



dejo
Jan 24, 2011, 11:13 PM
Wirelessly posted (Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148a Safari/6533.18.5)

There is no garbage-collection in iOS.

PhoneyDeveloper
Jan 24, 2011, 11:45 PM
The usual reason for a crash when an autorelease pool is drained is due to over-release of an object that was added to the autorelease pool.

You would be able to figure out what kind of object by using NSZombies. Also, Build and Analyze might figure it out for you.

I recommend that you don't use detachNewThreadSelector: Instead use NSOperation and NSOperationQueue. It generally works better and is easier to make work correctly.

xingworld
Jan 24, 2011, 11:59 PM
Wirelessly posted (Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148a Safari/6533.18.5)

There is no garbage-collection in iOS.

thank you very much!

The usual reason for a crash when an autorelease pool is drained is due to over-release of an object that was added to the autorelease pool.

You would be able to figure out what kind of object by using NSZombies. Also, Build and Analyze might figure it out for you.

I recommend that you don't use detachNewThreadSelector: Instead use NSOperation and NSOperationQueue. It generally works better and is easier to make work correctly.


Yes, I see, thank you very much for your reply!!!:)