MQX Resource Cleanup Concepts

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MQX Resource Cleanup Concepts

558 Views
michaelmeier
Contributor II

I have spent some time chasing down an elusive bug. At the heart of the bug lies MQX resource cleanup.

I have opened an ADC converter file as well as a number of files for its associated channels in one task, called main_task. After main_task initialized all the hardware and software components, it returns. Upon this return, MQX freed the memory that the ADC driver allocated. This is because the ADC driver allocated the memory with _mem_alloc_zero, so not with a _mem_alloc_system_... function. Even though the ADC driver's memory is freed, the ADC driver is _not_ stopped. Its intterupt routine happily continues write A/D samples into its supposed buffer, which is now memory in the memory allocator's free list and might later become memory for some other part of the application. Ultimately, this led to the system crashing.

I don't understand MQX's double personality when it comes to resource cleanup on task end or destroy. Memory allocated with _mem_alloc_...(), not with _mem_alloc_system_...() is freed. This matches documentation. Mutexes initialized with _mutex_init() get cleaned up on _task_destroy() via _mutex_cleanup(). The ADC driver, however, does not get stopped or cleaned up on task destroy.

What are MQX's concepts when it comes to resource cleanup? Which resources can I expect to be cleaned up?

0 Kudos
1 Reply

208 Views
Martin_
NXP Employee
NXP Employee

Hello,

section 3.3.6 Terminating Tasks in Freescale MQX RTOS User's Guide gives some information. User is responsible for destroying lightweight components. This can be tricky as it comes into I/O drivers, as many of them use lightweight components such as lightweight events or lightweight semaphores for their own operation.

3.3.6 Terminating Tasks

A task can terminate itself or any other task, whose task ID it knows. When a task is terminated, its children are not terminated. When a task is terminated, MQX frees the

task's MQX-managed resources. These resources include:

• dynamically allocated memory blocks and partition blocks

• message queues

• messages

• mutexes

• non-strict semaphores

• strict semaphores after posting them

• queued connections are dequeued

• task descriptor

Note The user is responsible for destroying all lightweight objects (lightweight semaphores, lightweight events,

lightweight timers, etc.) before terminating a task as this is not done by the MQX task termination functions!

0 Kudos