When a thread exits, it should not delete itself.
There are two problems with this:
1) Existing bugs due to continued referencing:
If you first set the thread to null and then do the deletion,
you run into problems with PrintHeapAllocation referencing TLS::current()
as part of the deletion.
If you first do the deletion and then set the thread to null, then
if you get an interrupt between them, you will dereference already
freed memory.
2) violating C++ sematics
Thanks to Lawrence Crowl and Dafe Detlefs for walking through this
semantic descussion:
"The this pointer has to be valid throughout the entire execution of
every member function. For that reasons, the code is invalid."
There are two problems with this:
1) Existing bugs due to continued referencing:
If you first set the thread to null and then do the deletion,
you run into problems with PrintHeapAllocation referencing TLS::current()
as part of the deletion.
If you first do the deletion and then set the thread to null, then
if you get an interrupt between them, you will dereference already
freed memory.
2) violating C++ sematics
Thanks to Lawrence Crowl and Dafe Detlefs for walking through this
semantic descussion:
"The this pointer has to be valid throughout the entire execution of
every member function. For that reasons, the code is invalid."