Dynamic allocation of queue creates conflict

j-md wrote on Monday, November 07, 2016:

Hi all,
I have an application running on Microchip dsPIC33 with FreeRTOS. I use the default options, that is, dynamic allocation everywhere for all FreeRTOS objects.
The application has 5 tasks and 3 queues of 20 items of 34 bytes each. The heap_3.c version is used.
I came across a bug, where the application would crash when accessing the queues. I eventually found that the 3 queues were created within one of the tasks, and when I looked at the addresses of the queues, they seem to overlap the stack of one of the tasks.
I moved the creation of the 3 queues to the main() function, and the problem disappeared. So my question is : how is the dynamic memory allocation handled? Is it forbidden to allocate a queue from within a task?

On another project using a STM32, I currently also have instabilities, and I now realize that one task is creating a recursive mutex. Could this be the same problem again? In any case I will try and create the mutex from the main() function to see if it does any good.

rtel wrote on Monday, November 07, 2016:

It is very common for tasks to create RTOS objects, and even other
tasks. As you are using heap_3 you will be using the malloc() provided
by your compiler. My best guess would be that the linker script is not
set up correctly for your device.

When you create RTOS objects (Queues, etc.) are you checking the return
value of the create function to check the object was actually created
before you use it?

If you change the order in which the object are created (by creating the
queues before you start the scheduler, as you said in your post) you are
not changing the amount of memory allocation, so I expect all you have
done is move the problem somewhere else.

j-md wrote on Monday, November 07, 2016:

Yes, I have checked the return value and if it is null, the processor goes into an endless loop so that I can easily locate the problem in debug mode. In the configuration file, the heap size is set to 15120 which seems to be enough for all the tasks and all the queues. But still, since I moved the queue creation to the main() function, It has worked without any problem.
When a task is created, does it allocate the size of the stack ? In my case, I doubted this is true.

davedoors wrote on Monday, November 07, 2016:

In the configuration file, the heap size is set to 15120

Which configuration file? FreeeRTOSConfig? Heap 3 doesnt use configTOTAL_HEAP_SIZE http://www.freertos.org/a00111.html