If chThdExit() never returns, and the compiler doesn't know this (which it seems like it will not), doesn't this imply that the cleanup code for user defined types (i.e., class destructors) will not be run, since the compiler implicitly places this code at the end the function body? So
vs.
would only be the same if all the local variables of the thread function have no destructor, i.e., built-in types or types which don't need to do anything special to clean up after themselves.
Specifically, here is a trivial made up example of what I am talking about:
Code: Select all
class foo {
public:
foo(int N) : data_ptr(new int[N]) {}
~foo() { delete [] data_ptr; }
private:
int * data_ptr;
};
msg_t thread_func(void *)
{
foo f(10);
chThdExit(0); /* Never returns, hence ~foo() is never called, hence memory leak of 10 ints on free store */
/* Code is placed implicitly here by the compiler to call ~foo(); but will never be executed because chThdExit never returns. */
}
if the call to chThdExit(0); was changed to return 0; then the implicit compiler generated calls to the destructor will be called and the memory will not be leaked. I'm not actually using new in my particular application, but I have some other cleanup code that needs to be called when a variable goes out of scope so I am pretty sure I need to use return, not chThdExit();
If I am missing something, let me know.
Luke