Article ID: 555563 - Last Review: February 11, 2006 - Revision: 1.0 Do not call "DisableThreadLibraryCalls" in a DLL which is statically linked to the CRT
SUMMARYIf you build a C/C++ DLL, which statically links to the multithreaded CRT, you should never call "DisableThreadLibraryCalls". This may lead to memory leaks if the caller of the DLL creates/destroys many threads. SYMPTOMSIf you have created a DLL which calls "DisableThreadLibraryCalls" to prevent the calling of "DllMain" for each thread creation/termination and you are linking against the multithreaded CRT, your process will leak memory if it creates and destroys many threads and calls your DLL from these threads. You will see a increase of "Private Bytes" of the process. The size of the leak depends on the used CRT. But the minimum for VC7.1 is about 140 bytes for VC8 about 532 bytes per thread. Depending on the used CRT functions it may even have much more allocated data. If your process creates and destroys many threads, this may lead to problems if you have a long running application. CAUSEFor several CRT functions it is necessary that the CRT is internally allocation memory on a per-thread basis. If a CRT functions needs to use this memory and this memory is not yet allocated for the current thread, it will allocate the required internal memory and store the pointer in a TLS- (Thread Local Storage) or FLS- (Fiber Local Storage) slot. These memory are normally release if you call "_endthread(ex)". But if the thread was created outside of the DLL, the statically CRT inside the DLL knows nothing about this thread. It just allocated memory if the internal memory is not allocated for the current thread. The only way to prevent this leak is to listen to the "DLL_THREAD_DETACH" in the "DllMain" callback. This function is called if a thread was destroyed. Then the CRT can look if it has allocated internal memory for this thread and can release it. Now if you call "DisableThreadLibraryCalls" you prevent the notification of "DLL_THREAD_DETACH" to the DLL. Therefore the CRT has no change to release the internal memory. RESOLUTIONTo resolve this problem, you have two choices:
MORE INFORMATIONThis article also applies to Microsoft Visual C++ .NET 2005 APPLIES TO
COMMUNITY SOLUTIONS CONTENT DISCLAIMERMICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES. | Other Resources Other Support Sites
CommunityArticle Translations
|






















Back to the top