gpt4 book ai didi

cocoa - EXC_BAD_指令: task_thread failed cocoa

转载 作者:行者123 更新时间:2023-12-03 17:29:43 26 4
gpt4 key购买 nike

我正在编写一个应用程序来打印从服务器收到的消息。我在不同的线程中分离监听函数:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
char *read;

do {
chk = (int) recv(connfd, read, 1, 0);

if(chk==-1) {
printf("Error in recv(): connfd = %d\n",connfd );
perror(err);
}
else if (chk ==0) {
[messagesView insertText:@"\nConnection closed by remote host\n"];
printf("Connection closed by remote host\n");
}
else {
if( *read == '\n') {
[messagesView insertText:@"\\n\n"];
printf("\\n");
}
else if (*read == '\r') {
[messagesView insertText:@"\\r\r"];
printf("\\r");
}
else {
[messagesView insertText:[NSString stringWithFormat:@"%c",*read]];
printf("%c", *read);
}
printf(" -- %d\n",*read);
}
} while (chk>0);

[pool drain];

chk 和 connfd 是 int,messagesView 是 NSTextView*。当我调用 [messagesView insertText:] 时,应用程序崩溃,并且在标题中收到错误。如果我评论所有这些调用,则应用程序可以正常工作,并且我可以在控制台中读取正确的消息。有什么建议吗?

最佳答案

辅助线程实际上不应该接触 GUI。您需要将信息传递回主线程上的对象并更新 TextView 。

来自Threading Programming Guide :

Threads and Your User Interface
If your application has a graphical user interface, it is recommended that you receive user-related events and initiate interface updates from your application’s main thread. This approach helps avoid synchronization issues associated with handling user events and drawing window content. Some frameworks, such as Cocoa, generally require this behavior, but even for those that do not, keeping this behavior on the main thread has the advantage of simplifying the logic for managing your user interface.

关于cocoa - EXC_BAD_指令: task_thread failed cocoa,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5704711/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com