阅读之前:你会看到我用来初始化缓冲区的 calloc 是导致问题的原因,但我仍然不知道为什么。静态定义缓冲区数组解决了这个问题,你会看到如果你继续阅读......
我正在编写一个由 2 个线程组成的 UDP 服务器:一个接收和解析线程。接收线程使用 recvfrom 侦听套接字并将接收到的消息推送到 received_msgs_buf 数组。解析线程从 received_msgs_buf 数组中弹出并决定如何处理它。
received_msgs_buf 数组受互斥体保护,信号量指示解析消息线程尝试从数组中弹出消息。问题是,每次我尝试将收到的消息推送到 received_msgs_buf 时,都会出现段错误。
这是我为缓冲区分配内存的方式:
// this is in the header file
extern UXIMessage::Wrapper* received_msgs_buf;
// this is in the main.cpp file that calls pthread_create()
UXIMessage::Wrapper* received_msgs_buf;
// This is in the init function for the receive thread, defined in the udp.cpp file
received_msgs_buf = (UXIMessage::Wrapper*)calloc(MAX_NUM_MSGS_IN_QUEUE, sizeof(UXIMessage::Wrapper));
这是我在接收线程中调用的推送函数:
void push_to_receive_buf(UXIMessage::Wrapper uxi_msg) {
pthread_mutex_lock(&received_msgs_mutex);
if( num_received_msgs < MAX_NUM_MSGS_IN_QUEUE ) {
printf("Message to put in buffer = %s\n", uxi_msg.DebugString().c_str());
printf("Num received messages = %d\n", num_received_msgs);
printf("Buf = %d\n", received_msgs_buf);
// THE FOLLOWING LINE SEGFAULTS
received_msgs_buf[num_received_msgs++] = uxi_msg;
}
pthread_mutex_unlock(&received_msgs_mutex);
sem_post(&received_msgs_sem);
}
从打印语句中我可以看到接收到的消息数已正确初始化为 0,接收到的消息完全有效并且缓冲区指针不为 NULL。这是打印出来的:
放入缓冲区的消息 = message_id: OCU_HEARTBEAT ocu_heartbeat { ocu_id: 4747 }
收到的消息数 = 0
缓冲区 = 778112
段错误发生在 CopyFrom() 函数中,该函数由 = 运算符调用。
编辑:已经很晚了,但我明天会尝试使用 C++ std::vector ......
Edit2:为澄清起见,互斥锁和信号量都在主函数中正确初始化,如下所示:
pthread_mutex_init(&received_msgs_mutex);
pthread_mutex_init(&msgs_to_send_mutex);
sem_init(&received_msgs_sem, 0, 0);
sem_init(&msgs_to_send, 0, 0);
EDIT3:问题是CALOC。当我静态定义 received_msgs_buf 如下:
// this is in the header file
extern UXIMessage::Wrapper received_msgs_buf[MAX_NUM_MSGS_IN_BUF];
// this is in the main.cpp file that calls pthread_create()
UXIMessage::Wrapper received_msgs_buf[MAX_NUM_MSGS_IN_BUF];
代码有效...有谁知道我对calloc做错了什么?