Arm Mbed and Pelion Device Management support forum

bug: Thread terminate does not cleanup memory.

As far as I’ve tested I’ve noted that the termination of a thread does not call the destructor of any classes that were created inside the function running the thread.
See the test code below and the output below that. I’d expect ~~Test to be printed there as well!

I’ve also tried using a pointer to the thread and delete that after terminating but I couldn’t get the destructor to be run. Does this mean that this is a memory leak? I.e. when I use a pointer and run the thread for a wile before terminating it and creating a new thread will leave all the stuff allocated inside the thread function in memory?
And how can I get the desired behaviour of calling the destructor being called?

<>
#include “mbed.h”

class Test {
public:
Test() { printf(“Test\n”); }
~Test() { printf("~Test\n"); }
};

Thread thread;

void led2_thread() {
Test test;
while (true) {
printf(“Hi!\n”);
rtos::ThisThread::sleep_for(100);
}
}

int main() {
thread.start(led2_thread);

rtos::ThisThread::sleep_for(500);
printf(“Terminate: %ld\n”, thread.terminate());

while (true) {
}
}
<>

<>
Test
Hi!
Hi!
Hi!
Hi!
Hi!
Terminate: 0
<
>

Hi,

The API says about terminate:
Terminate execution of a thread and remove it from Active Threads.
This requires a bit more investigation but so far I would use Threads keeping in mind that they are just stopped when terminate is called - not clear the Thread from the memory.

Regards,
Pekka