如何避免使用CreateThread函数导致的内存泄露

点击打开链接


前阵子翻了翻候杰老师翻译的那本Win32多线程的书,老书了,很经典。

    书里提到,当使用C/C++的时候,有些情况下CreateThread会导致内存泄露,建议使用_beginthread和_beginthreadex。这种泄露网上也有不少帖子在讲。

    事实上,这种泄露的可能是存在的,但是只要稍微注意一下,是可以找到方法避免的。

 

 

CreateThread导致内存泄露的原因

 

    这得从C运行时库说起了。

    VC运行时库,有一个宏errno,用来获得上一步操作的错误码,类似于Win32中的GetLastError()函数。在多线程环境下,不同线程调用errno返回的都是caller线程的错误码,绝对不会混淆,这是因为使用了TLS技术。

    TLS,Thread Local Storage,是用来存取线程相关数据的一种技术,在Win32中由操作系统的Tls*系列函数提供支持。例如,可以在程序开始的地方调用TlsAlloc()函数,获得一个TLS index,这个index在进程范围内有效,然后可以创建n个线程,在每个线程中使用TlsSetValue(index,data)将线程相关数据和index关联起来,使用TlsGetValue(index)来获取当前线程和index相关联的的线程相关数据。

    查看msdn可以发现,Tls*函数的定义如下:

[cpp]  view plain copy
  1. DWORD WINAPI TlsAlloc(void);  
  2.   
  3. BOOL WINAPI TlsSetValue(  
  4.   __in          DWORD dwTlsIndex,  
  5.   __in          LPVOID lpTlsValue  
  6. );  
  7.   
  8. LPVOID WINAPI TlsGetValue(  
  9.   __in          DWORD dwTlsIndex  
  10. );  
  11.   
  12. BOOL WINAPI TlsFree(  
  13.   __in          DWORD dwTlsIndex  
  14. );  

    观察TlsSetValue/TlsGetValue的原型可以发现,与index关联的数据只能是void *类型,因此通常的做法是在线程开始的时候,为这个线程分配一块内存,用于存储所有与线程相关的数据,然后把这块内存的起始地址与index关联起来。如果这块内存在线程退出的时候没有释放掉,那就有内存泄露的危险。

 

    回到errno,来看看C运行时库是如何实现errno的。

    errno的声明和实现如下:

[c-sharp]  view plain copy
  1. /* error.h - errno的声明 */  
  2. _CRTIMP extern int * __cdecl _errno(void);  
  3. #define errno   (*_errno())  
  4.   
  5. /* dosmap.c - errno的实现 */  
  6. int * __cdecl _errno(  
  7.         void  
  8.         )  
  9. {  
  10.     _ptiddata ptd = _getptd_noexit();  
  11.     if (!ptd) {  
  12.         return &ErrnoNoMem;  
  13.     } else {  
  14.         return ( &ptd->_terrno );  
  15.     }  
  16. }  

    观察_errno的代码,函数首先调用了_getptd_noexit()函数,这个函数的代码如下:

[cpp]  view plain copy
  1. /* tiddata.c - _getptd_noexit()实现 */  
  2. _ptiddata __cdecl _getptd_noexit (  
  3.         void  
  4.         )  
  5. {  
  6.     _ptiddata ptd;  
  7.     DWORD   TL_LastError;  
  8.   
  9.     TL_LastError = GetLastError();  
  10.   
  11. #ifdef _M_IX86  
  12.   
  13.     /* 
  14.      * Initialize FlsGetValue function pointer in TLS by calling __set_flsgetvalue() 
  15.      */  
  16.   
  17.     if ( (ptd = (__set_flsgetvalue())(__flsindex)) == NULL ) {  
  18. #else  /* _M_IX86 */  
  19.     if ( (ptd = FLS_GETVALUE(__flsindex)) == NULL ) {  
  20. #endif  /* _M_IX86 */  
  21.         /* 
  22.          * no per-thread data structure for this thread. try to create 
  23.          * one. 
  24.          */  
  25. #ifdef _DEBUG  
  26.         extern void * __cdecl _calloc_dbg_impl(size_tsize_tintconst char *, intint *);  
  27.         if ((ptd = _calloc_dbg_impl(1, sizeof(struct _tiddata), _CRT_BLOCK, __FILE__, __LINE__, NULL)) != NULL) {  
  28. #else  /* _DEBUG */  
  29.         if ((ptd = _calloc_crt(1, sizeof(struct _tiddata))) != NULL) {  
  30. #endif  /* _DEBUG */  
  31.   
  32.             if (FLS_SETVALUE(__flsindex, (LPVOID)ptd) ) {  
  33.   
  34.                 /* 
  35.                  * Initialize of per-thread data 
  36.                  */  
  37.   
  38.                 _initptd(ptd,NULL);  
  39.   
  40.                 ptd->_tid = GetCurrentThreadId();  
  41.                 ptd->_thandle = (uintptr_t)(-1);  
  42.             }  
  43.             else {  
  44.   
  45.                 /* 
  46.                  * Return NULL to indicate failure 
  47.                  */  
  48.   
  49.                 _free_crt(ptd);  
  50.                 ptd = NULL;  
  51.             }  
  52.         }  
  53.     }  
  54.   
  55.     SetLastError(TL_LastError);  
  56.   
  57.     return(ptd);  
  58. }  

    _getptd_noexit()函数首先通过TLS查找线程相关数据,如果没有找到,就分配一块内存,存放_tiddata结构,并将这块内存与__flsindex相关联。由此可见,errno的确使用了TLS技术,而且通过查找_getptd_noexit() 可以发现,VC运行时库中很多很多函数都使用了TLS,errno只不过是其中的一个典型。

 

    可以猜测一下,当使用CreateThread函数创建线程后,线程函数并不会做C运行时库的初始化,而当线程函数调用errno或localtime或其他需要TLS支持的函数时,这些函数会调用_getptd_noexit()函数初始化一个VC运行时库的TLS数据,当线程函数退出时,这块内存不会自动释放,因此产生了泄漏。

 

    接下来看一下_beginthread/_beginthreadex函数,看看这两个函数如何处理TLS数据:

 

[cpp]  view plain copy
  1. _MCRTIMP uintptr_t __cdecl _beginthread (  
  2.         void (__CLRCALL_OR_CDECL * initialcode) (void *),  
  3.         unsigned stacksize,  
  4.         void * argument  
  5.         )  
  6. {  
  7.         _ptiddata ptd;  
  8.   
  9.         ...  
  10.   
  11.         __set_flsgetvalue();  
  12.   
  13.         if ( (ptd = (_ptiddata)_calloc_crt(1, sizeof(struct _tiddata))) == NULL )  
  14.         {  
  15.             ...  
  16.         }  
  17.   
  18.         ...  
  19.   
  20.         ptd->_initaddr = (void *) initialcode;  
  21.         ptd->_initarg = argument;  
  22.   
  23.         ...  
  24.   
  25.         if ( (ptd->_thandle = thdl = (uintptr_t)  
  26.               CreateThread( NULL,  
  27.                             stacksize,  
  28.                             _threadstart,  
  29.                             (LPVOID)ptd,  
  30.                             CREATE_SUSPENDED,  
  31.                             (LPDWORD)&(ptd->_tid) ))  
  32.              == (uintptr_t)0 )  
  33.         {  
  34.                 ...  
  35.         }  
  36.   
  37.         if ( ResumeThread( (HANDLE)thdl ) == (DWORD)(-1) ) {  
  38.                 ...  
  39.         }  
  40.   
  41.         ...  
  42. }  
  43.   
  44. static unsigned long WINAPI _threadstart (  
  45.         void * ptd  
  46.         )  
  47. {  
  48.         _ptiddata _ptd;  
  49.   
  50.         ...  
  51.   
  52.         if ( (_ptd = (_ptiddata)__fls_getvalue(__get_flsindex())) == NULL)  
  53.         {  
  54.             if ( !__fls_setvalue(__get_flsindex(), ptd) )  
  55.             {  
  56.                 ExitThread(GetLastError());  
  57.             }  
  58.         }  
  59.         else  
  60.         {  
  61.             ..  
  62.         }  
  63.   
  64.         ...  
  65.   
  66.         _callthreadstart();  
  67.   
  68.         return(0L);  
  69. }  
  70.   
  71. static void _callthreadstart(void)  
  72. {  
  73.     _ptiddata ptd;  
  74.   
  75.     ptd = _getptd();  
  76.   
  77.   
  78.     __try  
  79.     {  
  80.         ( (void(__CLRCALL_OR_CDECL *)(void *))(((_ptiddata)ptd)->_initaddr) )  
  81.             ( ((_ptiddata)ptd)->_initarg );  
  82.   
  83.         _endthread();  
  84.     }  
  85.     __except ( ... )  
  86.     {  
  87.         ...  
  88.     }  
  89. }  
  90.   
  91. void __cdecl _endthread (  
  92.         void  
  93.         )  
  94. {  
  95.         _ptiddata ptd;  
  96.   
  97.         ptd = _getptd_noexit();  
  98.         if (ptd) {  
  99.             if ( ptd->_thandle != (uintptr_t)(-1) )  
  100.                     (void) CloseHandle( (HANDLE)(ptd->_thandle) );  
  101.             _freeptd(ptd);  
  102.         }  
  103.   
  104.         ExitThread(0);  
  105. }  
 

    以_beginthread函数为例,如上所示的精简后的代码,整个流程如下:

      1. 分配一块内存ptd用于存储_tiddata,并将之初始化,用户所指定的线程函数和参数被存放于ptd中

      2. 创建线程,线程的启动函数为_threadstart函数,参数为ptd

      3.  _threadstart函数在将ptd设置为TLS数据后,调用_callthreadstart()函数

      4. _callthreadstart()函数调用用户指定的线程函数,并传入用户指定的参数,然后调用_endthread()函数

      5. _endthread()中,调用_freeptd(ptd),释放步骤1中分配的ptd

 

    由此可见:

      1. 使用_beginthread(ex)的理由在于这个函数对CRT的TLS数据进行了适当的分配和释放操作,避免内存泄露

      2. 内存泄露存在的原因,是由CreateThead创建的线程不会去检查CRT的TLS数据是否需要释放

 

 

适当的处理

 

    在了解了CreateThread导致内存泄露的原因后,我简单考虑了一下避免这种内存泄露的方法。

 

    首先老老实实的使用_beginthead(ex)函数,是最稳妥的办法。

    其次能否避免使用CRT中依赖TLS的函数呢?

    也许可以,但是我们所书写的代码,不完全是我们自己在用,而且我们CreateThead所创建的线程,也不一定跑的都是自己的代码,例如我们提供一个库给别人使用,难道还要特别说明不允许使用errno/localtime等函数么?因此这个方法是不建议的。

 

    如果我想用CreateProcess,或者我所使用的底层库使用的是CreateProcess函数,我又不可避免的会使用依赖于TLS的VC运行时库函数,有什么办法能保证ptd会被释放呢?

    我们可以自己释放ptd。前面的分析可以看出,_endthread()函数调用了_freeptd(ptd)来释放ptd,因此我们可以在线程函数的末尾显示的调用_endthread()或_endthreadex(retcode)函数来释放ptd。在查看了_freeptd函数的代码后,我发现如果传入参数是NULL,_freeptd函数释放的就是caller线程的ptd,因此也可以直接调用_freeptd来执行清理。

    另外,我们也可以自动释放ptd。在VC的工程属性中,可以选择运行时库的类型,如图:

    如果我们选择/MTd或/MDd,运行时库以动态方式链接,即我们的程序会使用传说中的msvcrt*.dll。在这个dll的入口函数中,会在DLL_THREAD_ATTACH时为attach的线程初始化TLS数据,在DLL_THREAD_DETACH时为detach的线程调用_freeptd函数执行清理。因此如果我们使用VC的动态库,使用CreateThread和使用_beginthread是同样安全的。

 

    总结一下,避免CreateThread引发泄露,大致有几种方法:

      1. 使用_beginthread/_beginthreadex函数创建线程

      2. 在线程函数return前,显示调用_endthread/_endthreadex函数

      3. 在线程函数return前,显示调用_freeptd(NULL),此方法在C语言中有效

      4. 使用/MTd或/MDd参数

 

 

补充

    如果没有特殊的理由,一定不要使用TerminateThread函数来终止线程。在连接静态库的情况下,TerminateThread会使线程直接被终止,因此没有释放ptd的机会;在连接静态库的情况下,DLL_THREAD_DETACH只有在线程正常退出时才会产生,因此被终止的线程的ptd同样不会被释放。

    引用的代码请参考VC crt的源代码。

    由于分析得比较粗略,难免有错误,欢迎指正。



猜你喜欢

转载自blog.csdn.net/chexlong/article/details/46425807