Thread: AfxBeginThead leaking or not...

  1. #1
    Registered User
    Join Date
    Jan 2005

    AfxBeginThead leaking or not...


    Suppose you are creating a simple multithreaded application in MS visual C/C++ (ver.6), Now please consider the following cases:

    CASE 1:

    A Non-MFC application, where the Thread Function calls one or more C Run-time functions.
    According to documentation we should use C Run-time function _beginthread , instead of directly calling WIN API Thread functions, to prevent memory leak. Fine.

    CASE 2:

    The same application now MFC. Thread Function calls one or more C Run-time functions, so donít use WIN API directly. Documentation also says donít use _beginthread on MFC, use AfxBeginThread.

    Hmm! Isnít AfxBeginThread is just a wrapper for the CreateThread API function. Does it take care about those memory leak ( calling C Run-times in Thread function, remember) ?

    Oh! Iím surely missing something here. What do you guys think?

  2. #2
    Registered User
    Join Date
    Sep 2004
    AfxBeginThread doesn't call CreateThread(), it calls CWinThread::CreateThread(). I'm guessing the implementation is different, and without memory leaks.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Thanks bithub.

    I just want to you to know, I posted the same question in MSDN.
    And this is what Doug Harrison[Microsoft MVP - Visual C++] has to say about it:

    Click me to view.
    Last edited by playwin; 01-07-2005 at 10:18 AM.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    As Doug mentioned, CWinThread uses _beginthreadex() under the hood. As for the memory leak, allow me to regurgitate a previous posting of my own:

    The memory which is potentially leaked, comes from a per-thread structure that is allocated off the heap. In order to understand when the leak occurs, we need to know (a) when the structure is allocated, and (b) when the structure is free'd.

    When is it allocated?
    • When _beginthread[ex] is called.
    • When any CRT function is called that creates the global structure (so it can be used).

    When is it free'd?
    • When _endthread[ex] is called.
    • When _beginthread[ex] is used, _endthread[ex] is called for you auto-magically.
    • When linked to the CRT dynamically, the structure is free'd auto-magically.

    When is memory leaked?
    • When linking statically with the CRT -> and CreateThread() is used -> and the thread calls a CRT function that allocates the global structure -> and the thread does not call _endthread[ex].
    • When a DLL module calls DisableThreadLibraryCalls() -> then satisfies the first bullet.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL leaking?
    By g4j31a5 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2009, 01:47 AM
  2. Leaking Memory in a sendmail milter I wrote?
    By stevfletchcom in forum C Programming
    Replies: 8
    Last Post: 10-10-2008, 11:30 AM
  3. leaking mutexes, with no mutex in code
    By bling in forum Windows Programming
    Replies: 5
    Last Post: 10-08-2008, 11:33 AM
  4. Leaking Singleton
    By Mario F. in forum C++ Programming
    Replies: 6
    Last Post: 08-22-2007, 05:14 AM
  5. Leaking
    By shiver in forum Tech Board
    Replies: 4
    Last Post: 07-05-2006, 07:22 AM