VS debug the program _ ASSERTE (_ CrtIsValidHeapPointer (pUserData) Crash cause and solution, _ assertepuserdata

Source: Internet
Author: User

VS debug the program _ ASSERTE (_ CrtIsValidHeapPointer (pUserData) Crash cause and solution, _ assertepuserdata

Debug the program. The program crashes when the dynamically applied memory is released using free or delete. The following dialog box is displayed:


Click Retry to find the specific CRT source code: _ ASSERTE (_ CrtIsValidHeapPointer (pUserData ));

1. Cause Analysis:

Check the source code of the CRT and check what is done in it step by step: _ CrtIsValidHeapPointer -----> _ CrtIsValidPointer ----> HeapValidate

First, check whether the pointer is valid in _ CrtIsValidPointer, that is, it is not empty. Then, call WIndows API HeapValidate. This is the key, in the CRT code, C ++'s lenet new calls the C language's malloc, and delete calls the C language's free. Because C-library functions are cross-platform, the significance of CRT is to encapsulate win api on Windows platform to implement these functions.

The HeapValidate function is used to check whether a memory pointer is applied for on this stack. Each PE file will initialize the CRT Runtime Library during loading and call heapinit. in c, _ heap_init and _ heap_init still use the win api HeapCreate to initialize the stack, and then save the stack handle to a global variable, in the future, this handle will be used for applying to release memory on the stack. The cause of the above crash is that the memory release pointer is not applied for on the stack of the PE file.

Specifically, my problem is that I applied for a piece of memory in the DLL and deleted it in the EXE. However, the two PES (exe and dll are both PE files) have different stack handles allocated during initialization, so an error occurs.

2. solution:

The cause of the problem is clear. In fact, we should note that the memory allocated on the stack is released on the stack.

Solution 1: The Memory applied in the DLL is released in the DLL. Do not release it from other modules. (to distinguish the second entry, the premise is that both exe and dll are compiled using mtd)

Solution 2: Allow all PES to share a stack handle, and all modules are compiled in mdd mode;

(In the Debug option above), the reason is that when mdd is used for compilation, all modules share the same msvcr *. dll and stack handle are stored as static variables. Therefore, all modules apply for and release memory on the same stack. No matter which module can release the memory applied by other modules. When mtd is used for compilation, all modules have their own independent mscvr *. dll (equivalent to put msvcr *. dll), so each module has its own stack when loading, then delete the memory applied by other modules, stack detection of this pointer is definitely invalid.

3. Postscript

The code word is so tired that it is difficult to understand Chinese literature. Thank you for your help.

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.