问题
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