Free TLS pointers for every thread

Viewed 978

Reading the following page in MSDN:

Using Thread Local Storage in a Dynamic-Link Library

I can't understand who is responsible for freeing the memory pointed by the TLS slot of each thread in the case of a FreeLibrary() call.

From what I understand, if I have several running threads, all of them allocated memory inside a TLS slot in a given index. When FreeLibrary() is called, only DLL_PROCESS_DETACH is triggered, so only the thread which receives the DLL_PROCESS_DETACH notification has a chance to call LocalFree() on its own data stored in the TLS slot, before calling TlsFree() on the index. This causes a memory leak in all of the other threads that don't have a chance to call LocalFree() on their data, as they don't receive a DLL_THREAD_DETACH notification.

Can someone explain when and where the buffers stored in TLS slots of each thread should be freed?

1 Answers
Related