因此,我有一个应用程序可以进行大量数学运算,但仅当我使用全局调度队列进行多线程时它才会崩溃。我想我可能做错了。谁能解释为什么这会导致崩溃?或者至少我可以如何尝试调试它。
如果我这样做,它会很好地打印出答案。我用仪器查看了代码,它没有泄漏或任何东西。
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];
});