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
<