我正在编写一个应用程序来打印从服务器收到的消息。我分开在不同的线程的聆听功能:EXC_BAD_INSTRUCTION:task_thread失败可可
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:]
时,应用程序崩溃,并且我收到标题中的错误。如果我评论所有这些调用,应用程序工作正常,我可以在控制台中阅读正确的消息。 有什么建议吗?
您是否使用Grand Central Dispatch? – 2011-04-18 18:21:57