所以,我有一个应用程序可以进行大量数学运算,但是当我使用Global Dispatch Queue进行多线程时它只会崩溃。我想我可能做错了。任何人都可以解释为什么这会导致崩溃?或者至少我可以尝试调试它。
如果我这样做,它打印出答案很好。我用仪器查看了代码,它没有泄漏或任何东西。
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];
});
答案 0 :(得分:0)
我将我的问题跟踪到非线程安全编程问题。有关详细信息,请参阅此处:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html。