调度全局队列崩溃



所以,我有一个应用程序可以进行大量计算,但它只在我使用全局调度队列进行多线程时崩溃。我想我可能做错了。有人能解释一下为什么会发生车祸吗?或者至少我可以尝试如何调试它。

如果我这样做,它会很好地打印出答案。我用仪器查看了代码,它没有泄漏或任何东西。

NSStringEncoding encoding;
NSError *error;
//Read in data file
NSString *Data1FileContent = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data1" ofType:@"txt"] usedEncoding:&encoding error:&error];
NSMutableArray *strides = [[Data1FileContent componentsSeparatedByString:@"n"] mutableCopy];
//Read in data file
NSString *Data2FileContent = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data2" ofType:@"txt"] usedEncoding:&encoding error:&error];
NSMutableArray *gaitTimes = [[Data2FileContent componentsSeparatedByString:@"n"] mutableCopy];
//Math
NSMutableArray *result = [gaitlyapunov gaitlyapunov:strides withTimeSteps:gaitTimes withFreq:150 withSegmentApproach:@"strides"];
NSLog(@"result = %@", result);
[gaitTimes release];
[strides release];
});

但是,当我尝试更新代码中的UILabel时,它会导致崩溃。

错误:

(lldb) //Thrown randomly in the math

导致问题的代码:

//Create a queue
dispatch_async(dispatch_get_global_queue(0, 0), ^{
    //This prints out fine
    dispatch_async(dispatch_get_main_queue(), ^{
        statusText.text = [NSString stringWithFormat:@"processing..."];
    });

NSStringEncoding encoding;
NSError *error;
//Read data
NSString *Data1FileContent = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data1" ofType:@"txt"] usedEncoding:&encoding error:&error];
NSMutableArray *strides = [[Data1FileContent componentsSeparatedByString:@"n"] mutableCopy];
//Read data
NSString *Data2FileContent = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data2" ofType:@"txt"] usedEncoding:&encoding error:&error];
NSMutableArray *gaitTimes = [[Data2FileContent componentsSeparatedByString:@"n"] mutableCopy];
//Math, that causes crash in this code only
NSMutableArray *result = [gaitlyapunov gaitlyapunov:strides withTimeSteps:gaitTimes withFreq:150 withSegmentApproach:@"strides"];
    //This should print the result into the label
    dispatch_async(dispatch_get_main_queue(), ^{
            statusText.text = [NSString stringWithFormat: @"result: %@", result];
    });
[gaitTimes release];
[strides release];
});

我将问题归结为非线程安全编程问题。有关更多信息,请参阅此处:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html.

最新更新