Hello,
I'm using the MQX.4.1.1/mqx/examples/cplus as a test vehicle.
I have modified it to simulate a task being aborted to test if the heap allocations for the task are freed.
Here is the only modification to the cplus.cpp file:
void cplus_task
(
uint32_t initial_data
)
{
{ // Scope for local to destruct
HelloWorld local;
HelloWorld *heap;
global.print("global");
local.print("local");
heap = new HelloWorld;
if (heap != 0) {
heap->print("heap");
#if 1 //DES 1=test, 0=default code
goto abort_now;
#else
delete heap;
#endif
} else {
_io_printf("heap: new failed\n");
} /* Endif */
} // local should destruct
_io_fflush(stdout);
#if 1 //DES 1=test, 0=default code
abort_now:
_task_abort(_task_get_id());
#else
_task_block();
#endif
}
The stack, variables and code step are shown:
The MQX Light Memory Pools has the Owner as System with Type 0x1fff:
It seems that for the lwmem alloc process which first jumps into a C++ library before returning to MQX to allocate memory but the block_ptr->U.S.TASK_NUMBER in lwmem.c is set to 0xffff rather than 0x0003. When the _task_abort() runs, the heap does not free the heap space that is allocated during the "heap = new HelloWorld;" code.
If I "hack" the lwmem alloc so the block_ptr->U.S.TASK_NUMBER is 0x0003, the Heap Owner is task 0x10003 and the heap frees correctly when _task_abort() runs.
Is this a C++ issue?
Regards,
David
Solved! Go to Solution.
Not an issue.
In current MQX C++ implementation user in this case need to call delete on all allocated objects in task as in standard C++.
This solve memory leaks.
If user want's to avoid of using deallocation of created objects, he can try to change in file comp.c malloc function change call from _mem_alloc_system() to _mem_alloc(). Then all allocated objects in task will be assigned to parent task.
Not an issue.
In current MQX C++ implementation user in this case need to call delete on all allocated objects in task as in standard C++.
This solve memory leaks.
If user want's to avoid of using deallocation of created objects, he can try to change in file comp.c malloc function change call from _mem_alloc_system() to _mem_alloc(). Then all allocated objects in task will be assigned to parent task.
Thank you Matus for your help and education.
Regards,
David