1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Crashed by using NSThread, help me, thanks

Discussion in 'iPhone/iPad Programming' started by xingworld, Jan 24, 2011.

  1. xingworld, Jan 24, 2011
    Last edited by a moderator: Jan 24, 2011

    macrumors newbie

    #1
    I dev for iphone, My code is:
    ----------------------------------------------------------------------------------------------------

    Code:
    - (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)
        {
        }
    
    
        [COLOR="Red"]// If removes the following line, program will not crash[/COLOR]
        [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!
     
  2. Moderator

    dejo

    Staff Member

    #2
    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.
     
  3. macrumors 68030

    PhoneyDeveloper

    #3
    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.
     
  4. xingworld, Jan 24, 2011
    Last edited by a moderator: Jan 25, 2011

    macrumors newbie

    #4
    thank you very much!


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

Share This Page