Sending a structure that has an character array using xQueueSend()

saadsaeed wrote on Friday, September 14, 2018:

Hello.
I have a simple problem I want to ask
I am sending a struct through freertos queue

//Structure to pass to a queue
typedef struct
{
	char mess[2000];
	int id;
}queue_message_passing_struct

When I have copied the contents into struct members and send it the board gets hanged. But, If i sue a pointer like this

//Structure to pass to a queue
typedef struct
{
	char mess*;
	int id;
}queue_message_passing_struct

It works FINE. What is the reason behind this and how should I fix this.

jeronimo479 wrote on Friday, September 14, 2018:

Queues copy data into the queue buffer. If the message you are inserting into the queue, is bigger than the queue, it won’t work. Your pointer to mess is only 4 byes. Your first queue_message_passing_struct is over 2004 bytes not including the overhead used by FreeRTOS. Your second queue_message_passing_struct is only 8 bytes.

Post the code you used to create the Queue.

saadsaeed wrote on Saturday, September 15, 2018:

//Structure to pass to a queue
typedef struct
{
    char mess[2000];
    int id;
}queue_message_passing_struct
// Creating an instance of a struct
queue_message_passing_struct mess;
//Creating a queue handle
QueueHandle_t xQueue1;
//Some function in which queue is created
void some_function_to_create_queue()
{
xQueue1 = xQueueCreate(1,sizeof(mess));
}

richarddamon wrote on Saturday, September 15, 2018:

Do you check that the value of xQueue1 is not 0 after the create. If your heap doesn’t have enough spare room to make that big of a structure, it will return 0, and if you don’t detect and report that, when you use that 0 as a queue handle problems will occur.

saadsaeed wrote on Saturday, September 15, 2018:

It return pdPASS

richarddamon wrote on Saturday, September 15, 2018:

xQueueCreate does NOT return a status flag, but a queue handle, so xQueue1 could NOT be pdPASS (which is the value 1). If by ‘pdPASS’, you mean it was not 0, then that is good.

saadsaeed wrote on Sunday, September 16, 2018:

Sorry, It return PDPASS when I send the data using xQueueSend()

richarddamon wrote on Sunday, September 16, 2018:

That doesn’t answer the question then. When creating the queue, did you check that the queue handle was not 0 aftger the create? (your shown code doesn’t have a test)

A second issue also could be do the sending and receiving task have enough stack space. If the structure is stored on their stack, it will require 2004 bytes of statck space, and if that is not available you could be overruning it and corrupting something else in memory.