Heap corrupting when using xQueue

x-8973
Posts: 20
Joined: Tue Apr 03, 2018 4:49 pm

Re: Heap corrupting when using xQueue

Postby x-8973 » Tue Jun 26, 2018 6:29 am

I used xQueue according to the documentation and the official example.

Code: Select all

uint32_t ulVar = 10UL;

 void vATask( void *pvParameters )
 {
 QueueHandle_t xQueue1;

	// Create a queue capable of containing 10 uint32_t values.
	xQueue1 = xQueueCreate( 10, sizeof( uint32_t ) );

	if( xQueue1 != 0 )
	{
		// Send an uint32_t.  Wait for 10 ticks for space to become
		// available if necessary.
		if( xQueueSend( xQueue1, ( void * ) &ulVar, ( TickType_t ) 10 ) != pdPASS )
		{
			// Failed to post the message, even after 10 ticks.
		}
	}
 }

OneTwo
Posts: 20
Joined: Mon Jan 15, 2018 9:24 am

Re: Heap corrupting when using xQueue

Postby OneTwo » Tue Jun 26, 2018 6:35 am

x-8973 wrote:I used xQueue according to the documentation and the official example.

Code: Select all

uint32_t ulVar = 10UL;

 void vATask( void *pvParameters )
 {
 QueueHandle_t xQueue1;

	// Create a queue capable of containing 10 uint32_t values.
	xQueue1 = xQueueCreate( 10, sizeof( uint32_t ) );

	if( xQueue1 != 0 )
	{
		// Send an uint32_t.  Wait for 10 ticks for space to become
		// available if necessary.
		if( xQueueSend( xQueue1, ( void * ) &ulVar, ( TickType_t ) 10 ) != pdPASS )
		{
			// Failed to post the message, even after 10 ticks.
		}
	}
 }
Ok, but a pointer fits into a uint32_t variable but not inside a uint8_t?!

4 Bytes vs 1 Byte

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Heap corrupting when using xQueue

Postby ESP_Angus » Tue Jun 26, 2018 7:20 am

x-8973 wrote:I used xQueue according to the documentation and the official example.
Can you please post a full compile-able sample which doesn't work? Services like https://pastebin.com or https://gist.github.com are good for this.

Please seriously consider the possibility that there's a bug in your code somewhere else, which is corrupting memory in or near a queue and causing the queue to crash. Queues are used by nearly every ESP-IDF user, and as far as I know this is the only report saying that basic queue functionality is broken. It's entirely possible you have found a bug, but there are some other explanations which would be good to rule out first.

chegewara
Posts: 2378
Joined: Wed Jun 14, 2017 9:00 pm

Re: Heap corrupting when using xQueue

Postby chegewara » Tue Jun 26, 2018 7:58 am

x-8973 wrote:I used xQueue according to the documentation and the official example.

Code: Select all

uint32_t ulVar = 10UL;

 void vATask( void *pvParameters )
 {
 QueueHandle_t xQueue1;

	// Create a queue capable of containing 10 uint32_t values.
	xQueue1 = xQueueCreate( 10, sizeof( uint32_t ) );

	if( xQueue1 != 0 )
	{
		// Send an uint32_t.  Wait for 10 ticks for space to become
		// available if necessary.
		if( xQueueSend( xQueue1, ( void * ) &ulVar, ( TickType_t ) 10 ) != pdPASS )
		{
			// Failed to post the message, even after 10 ticks.
		}
	}
 }
Im looking on this and i cant believe, can this be true? You have task with no infinite loop and without vTaskDelete(). It will crash with strange error like heap corruption and there is no chance to debug it.
To be honest i suspected it 2 days ago or so, but without code i didnt want to open my mouth.

x-8973
Posts: 20
Joined: Tue Apr 03, 2018 4:49 pm

Re: Heap corrupting when using xQueue

Postby x-8973 » Tue Jun 26, 2018 8:11 am

chegewara, the above code is an excerpt from the file queue.h. This is NOT my code :)

OneTwo, I looked at the documentation on the freertos.org website:

pvItemToQueue A pointer to the item that is to be placed on the queue. The size of the items the queue will hold was defined when the queue was created, so this many bytes will be copied from pvItemToQueue into the queue storage area.

I pass a pointer over which the *sizeof(uint8_t)* bytes will be copied.

ESP_Angus, I can send the compiled files when I'm at home. What files are needed?

OneTwo
Posts: 20
Joined: Mon Jan 15, 2018 9:24 am

Re: Heap corrupting when using xQueue

Postby OneTwo » Tue Jun 26, 2018 8:31 am

x-8973 wrote:chegewara, the above code is an excerpt from the file queue.h. This is NOT my code :)

OneTwo, I looked at the documentation on the freertos.org website:

pvItemToQueue A pointer to the item that is to be placed on the queue. The size of the items the queue will hold was defined when the queue was created, so this many bytes will be copied from pvItemToQueue into the queue storage area.

I pass a pointer over which the *sizeof(uint8_t)* bytes will be copied.

ESP_Angus, I can send the compiled files when I'm at home. What files are needed?

:roll:

I mean when you create a Queue like this

Code: Select all

	systemEvtQueue = xQueueCreate(10, sizeof(uint8_t));
you create a queue which can hold 10 Items with the size of 1 Byte, right?

But later you store a pointer inside of this queue which has at least a size of 4 Bytes.

Code: Select all

	xQueueSend(systemEvtQueue, (void *)&event, (TickType_t)0);

x-8973
Posts: 20
Joined: Tue Apr 03, 2018 4:49 pm

Re: Heap corrupting when using xQueue

Postby x-8973 » Tue Jun 26, 2018 8:37 am

If I understand correctly from the documentation, the pointer is stored inside the queue, but the copied value. Therefore, a pointer is passed to xQueueSend(): the function copies the required number of bytes from the memory by this pointer. The function xQueueReceive() also sends a pointer to the memory where the value of the element is copied from the queue.

I pass exactly the value of the variable. Precisely because at the time of reception in another RTOS task the pointer can being incorrect.

OneTwo
Posts: 20
Joined: Mon Jan 15, 2018 9:24 am

Re: Heap corrupting when using xQueue

Postby OneTwo » Tue Jun 26, 2018 8:49 am

x-8973 wrote:If I understand correctly from the documentation, the pointer is stored inside the queue, but the copied value. Therefore, a pointer is passed to xQueueSend(): the function copies the required number of bytes from the memory by this pointer. The function xQueueReceive() also sends a pointer to the memory where the value of the element is copied from the queue.

I pass exactly the value of the variable. Precisely because at the time of reception in another RTOS task the pointer can being incorrect.
I can't imagine that and if so then just a flat copy and not a deep copy.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Heap corrupting when using xQueue

Postby ESP_Angus » Tue Jun 26, 2018 11:34 pm

x-8973 wrote:If I understand correctly from the documentation, the pointer is stored inside the queue, but the copied value. Therefore, a pointer is passed to xQueueSend(): the function copies the required number of bytes from the memory by this pointer. The function xQueueReceive() also sends a pointer to the memory where the value of the element is copied from the queue.

I pass exactly the value of the variable. Precisely because at the time of reception in another RTOS task the pointer can being incorrect.
That's correct. And the number of bytes copied to/from the pointer address is the item size passed in when the queue is created.
OneTwo wrote: ESP_Angus, I can send the compiled files when I'm at home. What files are needed?
Sorry, we can't debug your project for you. But if you have some simple example of code which crashes, we can look at it. What I meant by "compile-able" is a full example of source code which I can put in a project and build - as opposed to an extract which is missing header files, other necessary functions, etc.

x-8973
Posts: 20
Joined: Tue Apr 03, 2018 4:49 pm

Re: Heap corrupting when using xQueue

Postby x-8973 » Wed Jun 27, 2018 2:39 am

I understood you. Well, I'll try to reproduce my problem in a simpler example.

Who is online

Users browsing this forum: Baoshi and 115 guests