NSThread 数据损坏
我有一个线程被调用 10 次:
[NSThread detachNewThreadSelector:@selector(workInBackground:) toTarget:self withObject:sendArray];
这是“workInBackground”方法:
- (void)workInBackground:(NSArray*)dataArray{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // create release pool
// display dataArray sent from Main Thread
NSLog(@"%i, %@", [[dataArray objectAtIndex:0] intValue], [dataArray objectAtIndex:1]);
// Fetch data from URL
NSString *myURL = [NSString stringWithFormat:@"http://somesite.com/index.php?s=%@", [dataArray objectAtIndex:1]];
NSURL *url = [[NSURL alloc] initWithString:myURL];
NSString *strResult = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
// display dataArray sent from Main Thread ... AGAIN
NSLog(@"this will show");
NSLog(@"%i, %@", [[dataArray objectAtIndex:0] intValue], [dataArray objectAtIndex:1]);
NSLog(@"this will not show");
// Code that returns super cool data to the main thread
[pool release]; // empty pool
我不能 100% 确定数据确实损坏,但我不明白为什么同一行代码 (NSLog) 会使应用程序崩溃。我必须在从 URL 获取之后使用 dataArray 中的数据,但不能。
我是 Objective C 的新手,所以如果我的错误很明显,我很抱歉:)
- 更新 - 发送到此方法的对象没有保留,这就是问题所在!
I have a Thread called 10 times:
[NSThread detachNewThreadSelector:@selector(workInBackground:) toTarget:self withObject:sendArray];
This is the "workInBackground" method:
- (void)workInBackground:(NSArray*)dataArray{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // create release pool
// display dataArray sent from Main Thread
NSLog(@"%i, %@", [[dataArray objectAtIndex:0] intValue], [dataArray objectAtIndex:1]);
// Fetch data from URL
NSString *myURL = [NSString stringWithFormat:@"http://somesite.com/index.php?s=%@", [dataArray objectAtIndex:1]];
NSURL *url = [[NSURL alloc] initWithString:myURL];
NSString *strResult = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
// display dataArray sent from Main Thread ... AGAIN
NSLog(@"this will show");
NSLog(@"%i, %@", [[dataArray objectAtIndex:0] intValue], [dataArray objectAtIndex:1]);
NSLog(@"this will not show");
// Code that returns super cool data to the main thread
[pool release]; // empty pool
I'm not 100% sure that data does corrupt but I don't understand why does the same line of code (NSLog) crash the app. I have to use data from my dataArray after the Fetch from URL but can't.
I'm new to Objective C so I'm sorry if my error is obvious :)
-- UPDATE -- object sent to this method wasn't retained and that was the problem!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如何创建和管理发送到后台线程的数组?
如果发生崩溃,请发布回溯。
您的解决方法不是解决办法,它只是缩小了任何导致应用程序崩溃的竞争条件的窗口。
最好的猜测(缺少显示如何创建数组的代码)是,在后台线程处理数组和字符串之前,主线程正在释放您的数组。
为每个生成的线程保留一次数组,然后在使用完毕后在线程中释放它。
更好的是,使用 GCD + 块。
How do you create and manage the array sent to the background thread?
If you have a crash, post the backtrace.
Your workaround isn't a fix, it is just narrowing the window of whatever race condition is killing your app.
Best guess (lacking the code showing how the array is created) is that your array is being released by the main thread before the background thread is done with the array and strings.
Retain the array once for each thread that is spawned and then release it in the thread when you are done with it.
Better yet, use GCD + Blocks.