locked
CreateThread() - memory leaks RRS feed

  • Question

  • Hi,

    I've heard that using CreateThread() (unlike _beginthread()) alongside C++/C standard library functions will cause memory leaks.  So this means, if I use rand(), write(), etc.... I have to use _beginthread()?  What if I am only using the template classes from the C++ STL?

    Thanks,

    Joe

    Sunday, March 30, 2014 2:15 AM

Answers

  • On 3/29/2014 10:15 PM, Joseph Raar wrote:

    I've heard that using CreateThread() (unlike _beginthread()) alongside C++/C standard library functions will cause memory leaks.  So this means, if I use rand(), write(), etc.... I have to use _beginthread()?

    Or, better still, _beginthreadex. _beginthread has problems of its own.

    What if I am only using the template classes from the C++ STL?

    These may very well call C standard library functions internally - malloc(), if nothing else.


    Igor Tandetnik
    Sunday, March 30, 2014 2:36 AM
  • Hi,

    I've heard that using CreateThread() (unlike _beginthread()) alongside C++/C standard library functions will cause memory leaks.  So this means, if I use rand(), write(), etc.... I have to use _beginthread()?  What if I am only using the template classes from the C++ STL?

    Thanks,

    Joe

    http://en.cppreference.com/w/cpp/thread/thread



    Corsair Carbide 300R with window
    Corsair TX850V2 70A@12V
    Asus M5A99FX PRO R2.0 CFX/SLI
    AMD Phenom II 965 C3 Black Edition @ 4.0 GHz
    G.SKILL RipjawsX DDR3-2133 8 GB
    EVGA GTX 6600 Ti FTW Signature 2(Gk104 Kepler)
    Asus PA238QR IPS LED HDMI DP 1080p
    ST2000DM001 & Windows 8.1 Enterprise x64
    Microsoft Wireless Desktop 2000
    Wacom Bamboo CHT470M
    Place your rig specifics into your signature like I have, makes it 100x easier to understand!

    Hardcore Games Legendary is the Only Way to Play!

    Sunday, March 30, 2014 3:18 AM

All replies

  • On 3/29/2014 10:15 PM, Joseph Raar wrote:

    I've heard that using CreateThread() (unlike _beginthread()) alongside C++/C standard library functions will cause memory leaks.  So this means, if I use rand(), write(), etc.... I have to use _beginthread()?

    Or, better still, _beginthreadex. _beginthread has problems of its own.

    What if I am only using the template classes from the C++ STL?

    These may very well call C standard library functions internally - malloc(), if nothing else.


    Igor Tandetnik
    Sunday, March 30, 2014 2:36 AM
  • So, basically only the C standard library functions in conjunction with CreateThread and ExitThread cause memory leaks.  But because C++ might use the C Library underneath the hood, it's best to just use _beginthreadex() and _endthreadex()?

    Thanks,

    Joe

    Sunday, March 30, 2014 3:01 AM
  • Some more details for the sake of completeness:

    • C runtime functions my require per thread data for various reasons, for example rand() stores its state per thread.
    • This per thread data structure is automatically allocated by _beginthreadex
    • _beginthreadex changes the thread start function to its own function which calls your function and on return it deletes the per thread data

    What happens if you don't use _beginthreadex? You should use C/C++'s facilities to create threads but what if you don't have any control over the thread creation? Does it mean that you can't use any C/C++ code in such cases due to potential memory leaks?

    Well, it depends. The C runtime attempts to delete the per thread data even if you don't use _beginthreadex:

    • If you're using the DLL version of the CRT then its DllMain will process DLL_THREAD_DETACH notifications to delete the per thread data
    • On Win2003 and higher the CRT relies on FLS callbacks to delete per thread data. That means that in this situation that data will be deleted even if you don't use the DLL version of the CRT

    What's left? Well, WinXP + static CRT => memory leak. As far as I can tell WinXP doesn't support FLS so the CRT has no way to cleanup the mess in this case.

    Sunday, March 30, 2014 8:08 AM