Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create an Array of Protocol Buffer Messages in C
    primarykey
    data
    text
    <p>BEFORE YOU READ: The calloc you'll see that I use to initialize the buffer is what's causing the problem, but I still don't know why. Statically defining the buffer array fixes the problem as you'll see if you keep reading...</p> <p>I'm writing a UDP server that is made up of 2 threads: a receive and parse thread. The receive thread listens to the socket using recvfrom and pushes the messages it receives to the received_msgs_buf array. The parse thread pops from the received_msgs_buf array and decides what to do with it.</p> <p>The received_msgs_buf array is protected by a mutex, and a semaphore signals the parse message thread to try and pop a message off the array. The problem is, every time I try to push a message I received into the received_msgs_buf I get a segfault.</p> <p>Here is how I allocated memory for the buffer:</p> <pre><code>// 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)); </code></pre> <p>Here is my push function called in the receive thread:</p> <pre><code>void push_to_receive_buf(UXIMessage::Wrapper uxi_msg) { pthread_mutex_lock(&amp;received_msgs_mutex); if( num_received_msgs &lt; 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(&amp;received_msgs_mutex); sem_post(&amp;received_msgs_sem); } </code></pre> <p>From the print statements I can see that the number of received messages is properly initialized to 0, the received message is perfectly valid and the buffer pointer is not NULL. Here's the print out:</p> <p>Message to put in buffer = message_id: OCU_HEARTBEAT ocu_heartbeat { ocu_id: 4747 }</p> <p>Num received messages = 0</p> <p>Buf = 778112</p> <p>The segfault occurs in the CopyFrom() function, which is what's called by the = operator.</p> <p>Edit: It's late, but I'll try just using a C++ std::vector tomorrow...</p> <p>Edit2: For clarification, the mutexes and semaphores are all initialized properly in the main function as follows:</p> <pre><code>pthread_mutex_init(&amp;received_msgs_mutex); pthread_mutex_init(&amp;msgs_to_send_mutex); sem_init(&amp;received_msgs_sem, 0, 0); sem_init(&amp;msgs_to_send, 0, 0); </code></pre> <p>EDIT3: THE PROBLEM IS THE CALLOC. When I statically define the received_msgs_buf as follows:</p> <pre><code> // 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]; </code></pre> <p>the code works...does anyone know what I'm doing wrong with the calloc?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload