# Reading from message queue crashes

**URL:** <https://forums.freertos.org/t/reading-from-message-queue-crashes/4125>\
**Category:** Kernel\
**Created:** [September 22, 2009, 10:39am UTC](https://forums.freertos.org/t/reading-from-message-queue-crashes/4125 "2009-09-22T10:39:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 22, 2009, 10:39am UTC](https://forums.freertos.org/t/reading-from-message-queue-crashes/4125/1 "2009-09-22T10:39:11Z")

</div>

**[ktownsend](https://sourceforge.net/u/ktownsend/)** wrote on [Tuesday, September 22, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/b5f8723e/#465b):

I’m trying to set up a task that will regularly check the rx message queue on uart, and I have the ISR working properly, but whenever I try to read the contents of the rx message queue I receive the character, but the mcu crashes immediately afterwards.&nbsp; Also, if no character is input before xQueuePeek times out (500 ticks in this case) it will also crash.&nbsp; If I remove the code to check the rx queue, it works fine and happily writes to the uart every 100 ticks as expected.

I just ordered the freertos book (hopefully I’ll get it today), but if there something that I’m doing in this task code that I obviously shouldn’t be?&nbsp; (All I’m trying to do it send the char back out to the UART just to see if it works or not.)

&nbsp;&nbsp;&nbsp; static portTASK\_FUNCTION(vUART1Task, pvParameters \_\_attribute\_\_((unused)))  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static xQueueHandle rxQueue = NULL;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uart1GetRxQueue (&amp;rxQueue);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; signed portCHAR rxChar;  
&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (;😉  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /\* Check if anything is available in the rx queue \*/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (rxQueue)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (xQueuePeek(rxQueue, &amp;rxChar, (portTickType)500))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /\* Print the result on the uart port \*/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char \*feedback;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sprintf(feedback, &quot;Received: %c\r\n&quot;, rxChar);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uart1PutString(feedback, 10);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Transmit current tick on UART1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char \*output;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sprintf(output, &quot;Ticks: %d\r\n&quot;, xTaskGetTickCount());  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uart1PutString(output, 10);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vTaskDelay(100);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp; }

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 22, 2009, 10:48am UTC](https://forums.freertos.org/t/reading-from-message-queue-crashes/4125/2 "2009-09-22T10:48:10Z")

</div>

**[ktownsend](https://sourceforge.net/u/ktownsend/)** wrote on [Tuesday, September 22, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/b5f8723e/#19c7):

It seems that it isn’t actually the queues, but the sprintf that’s causing the problems.&nbsp; The following code seems to work as expected:

&nbsp;&nbsp;&nbsp; /\* Check if anything is available in the rx queue \*/  
&nbsp;&nbsp;&nbsp; if (rxQueue)  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (xQueueReceive(rxQueue, &amp;rxChar, (portTickType)500)) // portMAX\_DELAY))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /\* Print the result on the uart port \*/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uart1PutString(&quot;Received: &quot;, portMAX\_DELAY);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uart1PutChar(rxChar, portMAX\_DELAY);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uart1PutString(&quot;\r\n&quot;, portMAX\_DELAY);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp; }

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 22, 2009, 11:06am UTC](https://forums.freertos.org/t/reading-from-message-queue-crashes/4125/3 "2009-09-22T11:06:13Z")

</div>

**[woops\_](https://sourceforge.net/u/woops_/)** wrote on [Tuesday, September 22, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/b5f8723e/#7548):

char \*feedback;  
With this line you allocate a character pointer, but no memory to hold the string to which it points. The pointer is uninitialized.

sprintf(feedback, &amp;quot;Received: %c\r\n&amp;quot;, rxChar);  
With this line you are writing a dozen characters into an address pointed to by an uninitialized pointer, so it will more than likely crash.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 22, 2009, 11:15am UTC](https://forums.freertos.org/t/reading-from-message-queue-crashes/4125/4 "2009-09-22T11:15:37Z")

</div>

**[ktownsend](https://sourceforge.net/u/ktownsend/)** wrote on [Tuesday, September 22, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/b5f8723e/#3530):

Ooops … so busy looking at the queues that I don’t pay attention to what’s right in front of my eyes 🙂

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /\* Print the result on the uart port \*/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char feedback;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sprintf(feedback, &quot;Received: %c\r\n&quot;, rxChar);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uart1PutString(feedback, 10);

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 22, 2009, 11:50am UTC](https://forums.freertos.org/t/reading-from-message-queue-crashes/4125/5 "2009-09-22T11:50:46Z")

</div>

**[woops\_](https://sourceforge.net/u/woops_/)** wrote on [Tuesday, September 22, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/b5f8723e/#9bb4):

that should be better, but make sure you have enough stack space to hold the 80 character array.
