NSThread 数据损坏

发布于 2024-10-20 02:42:23 字数 1279 浏览 8 评论 0原文

我有一个线程被调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

战皆罪 2024-10-27 02:42:23

如何创建和管理发送到后台线程的数组?

如果发生崩溃,请发布回溯。


您的解决方法不是解决办法,它只是缩小了任何导致应用程序崩溃的竞争条件的窗口。

最好的猜测(缺少显示如何创建数组的代码)是,在后台线程处理数组和字符串之前,主线程正在释放您的数组。

为每个生成的线程保留一次数组,然后在使用完毕后在线程中释放它。

更好的是,使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文