MFC multi-threaded memory leak problem & workaround

Source: Internet
Author: User

In the interface programming with Visual Studio (such as MFC), the foreground UI can be implemented through the message loop mechanism of MFC. And for the data processing in the background, we may use multithreading to deal with. So for most people (especially my rookie), a more efficient way is to choose MFC Multithreading: AfxBeginThread or CreateThread to build multi-threaded. When one or two threads are still available, there is a high likelihood of a memory leak when 3 or more than 3 threads are present. The causes are analyzed as follows:

Multithreading Insecurity for CWinThread:

Because CWinThread calls _beginthreadex to initialize the C runtime library, and similarly, if the thread is forced to terminate (TerminateThread), because TerminateThread is not going to pipe the C run-time library, There was a problem with the memory release of the C run-time data that caused the partial and reference count related. The most typical feature is that static variable memory reclamation using an STL library will make an error, resulting in a false-positive exception when the process exits.
In addition, if AfxBeginThread frequently recycles and distributes threads, it can also cause crashes if not strictly manipulated. In VC6, we should strictly control the use of STL library, avoid the coexistence of MFC Library and STL Library, otherwise, there will be many problems.

Reason:

AfxBeginThread directly calls the CreateThread creation thread instead of the Beginthreadex function recommended under C, and the two functions are distinguished, mainly due to the historical legacy of the C run-time library.

in a multithreaded environment, there are problems with C + + run-time library variables and functions including errno, _doserrno, Strtok, _wcstok, Strerror, _strerror, Tmpnam, Tmpfile, Asctime, _ Wasctime, Gmtime, _ECVT and _FCVT.
to enable a multithreaded C + + program to run correctly, you must create a data structure and use it with theeach of the threads of the C + + run-time library function is associated. When you call the C + + run-time library, these functions must knowlook at the data block of the calling thread so that it does not adversely affect other threads. does the system know that the data block is allocated when a new thread is created? The answer is that it does not know. The system doesn't even know youThe resulting application is written in C + + , and it is not known that the thread in which you invoke the function is inherently unsafe. The problem is that youall operations must be performed correctly. To create a new thread, never call the operating system's CreateThread letter, you must call the C + + run-time library function _beginthreadex.
Here are some points about _beginthreadex:
each thread obtains its own tiddata memory structure, allocated by the stack of the C + + run-time library. (Tiddata structure bitIn the Visual C + + source code in the Mtdll.h file). The address of the thread function passed to _beginthreadex is stored in the Tiddata memory block. The arguments passed to the function are alsostored in the data block. _beginthreadex does call CreateThread internally, because this is how the operating system knows how to create a new thread
The only way. # # #可以看出调用_beginthreadex时分配了额外的内存空间.

What happens if you call CreateThread instead of calling the _beginthreadex of the C + + run-time library to create a new thread. When a thread calls a C + + run-time library function that requires a tiddata structure, some of the following conditions occur (most C + + run-time library functions are thread-safe functions and do not require this structure). First
First, the C + + run-time library function attempts to get the address of the thread's data block (by calling TlsGetValue). If you return
NULL as the address of the Tiddata block, the calling thread does not have a tiddata block associated with that address. At this point, the C/C + + run-time library function allocates a tiddata block to the calling thread in the field and initializes it. The Tiddata block (via TlsSetValue) is then associated with the line threads.
# # # #_beginthreadex相对应的推出函数是_endthreadex, this function will release the contents of Tiddata.
---------------------------
If you are using CreateThread to create threads, and you do not use the special run-time libraries mentioned above, there will be no problem; If you must use those runtime, it is best to call _beginthreadex this API to create threads, Otherwise, the TIDDATA data block cannot be undone, causing a memory leak.

You can refer to the _beginthreadex source code to understand.

Extended reading:

About the difference between _beginthreadex and CreateThread


MFC multi-threaded memory leak problem & workaround

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.