VS Debugger _asserte (_CrtIsValidHeapPointer (puserdata)) crash causes and workarounds

Source: Internet
Author: User

Debug program, the dynamic request for memory with free or delete when the program crashes, jump out of the following dialog box:


Click Retry to locate the specific CRT source code section: _asserte (_CrtIsValidHeapPointer (puserdata));

1, Reason analysis:

View CRT source code, step by step to see what is inside what it is: _crtisvalidheappointer----->_crtisvalidpointer---->heapvalidate

First, in the _CrtIsValidPointer to detect whether the pointer is valid, that is, not empty, and then call the Windows API HeapValidate, this is the key, the CRT code in C + + 's Le network new calls the C-language malloc, Delete calls the C language of free. Because C library functions are cross-platform, the meaning of the CRT is to encapsulate the win API on the Windows platform to implement these functions.

The HeapValidate function is to detect whether a memory pointer is applied on this stack. Each PE file initializes the CRT runtime at load time, invokes the win API heapcreate initialization stack in _heap_init,_heap_init in heapinit.c, and then saves the handle of the stack to a global variable. This handle is used by the subsequent process to request free memory on the stack. The reason for the crash above is because the pointer to free memory is not requested on the stack of the PE file.

Specifically, my problem is actually to request a piece of memory in the DLL, in the EXE to delete this block of memory. However, these two PE (EXE, DLL are PE files) are allocated at the initialization of the stack handle is not the same, so there is an error.

2. Solution:

The reason for the problem is clear, in fact, is to let us notice, on whose stack the memory is allocated on whose stack is released.

Scenario One: In the DLL application of memory, in the DLL is released, not to be released by other modules; (to differentiate the second, this premise is that EXE and DLL are compiled using MTD method)

Scenario Two: Let all the PE share a stack handle, all the modules are used in the MDD way to compile;

(This is the debug option), because when you compile with MDD, all modules share the same msvcr*.dll, and the stack handle is stored as a static variable, so all the modules are on the same stack requesting, freeing memory, No matter which module can release the memory requested by other modules. In the case of MTD compiling, all modules are equivalent to having their own independent mscvr*.dll (equivalent to packing the code needed in the Msvcr*.dll into their own), so each module has its own stack when it is loaded, which removes the memory requested by the other modules. The pointer to the stack detection is definitely invalid.

3. PostScript

Code word is tired, the literature is not good, said a bit around. I hope to be helpful to those who have encountered this problem, thank you.

Why

Vs Debugger _asserte (_CrtIsValidHeapPointer (puserdata)) crashes and how to troubleshoot

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.