SIGSEGV in background thread NSRunLoop runMode:beforeDate:

旧时模样 提交于 2019-12-11 09:08:45

问题


I am using background threads with following pattern:

// Defined in .h-file as ivar
BOOL backgroundThreadIsAlive;


// .m file
static NSThread *backgroundThread = nil;

- (id)init
{
    if (self = [super init])
    {       
        if (backgroundThread == nil)
        {
            backgroundThreadIsAlive = YES;
            backgroundThread = [[NSThread alloc] initWithTarget:self selector:@selector(threadMain:) object:nil];
            [backgroundThread start];
        }
    }

    return self;
}


- (void)threadMain:(id)data
{
    NSRunLoop *runloop = [NSRunLoop currentRunLoop];
    [runloop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];

    while (backgroundThreadIsAlive)
    {
        [runloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }
}

My application sometimes crash with SIGSEGV in the line

[runloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

I am using ARC. The crash is not reproducible. Just found it, when diving into testflight and saw this is the most often crash I got. It seems to appear independently from iOS version (I support iOS5+) and device type.

  • May someone have a hint for me, what I do wrong?
  • Is there a better solution doing background-threads? (maybe using GCD)
  • Is there a way to reproduce those issues?

Thank you for your time and guidiance. :)

来源:https://stackoverflow.com/questions/18760334/sigsegv-in-background-thread-nsrunloop-runmodebeforedate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!