The program encountered title run times error, refer to the following text, take the custom class's object definition to new mode generation after the problem resolution.
!! Expression: _CrtIsValidHeapPointer (Puserdata)
void Cimagerecview::onfilecolhistogram ()
{
Todo:add your command handler code here
Cimagerecdoc *pdoc = GetDocument ();
LPSTR Lpdib;
Colhistogram mycolhist;
Lpdib = (LPSTR):: GlobalLock ((HGLOBAL) Pdoc->gethdib ());
PMYCOLHIST->RGBTOHSV (LPDIB);
:: GlobalUnlock ((HGLOBAL) Pdoc->gethdib ());
}
The problem is in the red place, customizing a class
Change the above statement to
Colhistogram * pmycolhist;
Pmycolhist = new Colhistogram;
Yes, but now I don't know why.
This paragraph in the (MSDN)
The _crtisvalidheappointer function is used to ensure this a   ; Specific memory address is within the local heap. The "local" heap refers to the heap created and mana Ged by a particular instance of the C run-time Library. If A dynamically linked library (DLL) contains a static &NB Sp Link to the run-time Library, then it have its own in Stance of the run-time heap, and therefore its own heap, &N Bsp Independent of the application ' s local heap. When _DEBUG are not defined, calls   To _CrtIsValidHeapPointer is removed during preprocessing.
See this paragraph slightly feel a little bit of meaning, I in the program myself applied for the local heap, also have to generate dynamic connection Library of the Dib class, to connect the C runtime Library, then my Colhistogram instance must be dynamically generated, because it does not have a corresponding heap in the C run-time library. For example, I add CString str, the program will not have a problem, but I only know that CString is a system-defined, and the C run-Library has nothing to do with it. If you statically link the C runtime, then the DLL will have a local heap independent of the application (the EXE that called it) (but my program does not), and if _DEBUG is not defined, the _CrtIsValidHeapPointer is removed by the preprocessor. That's probably the way it is, I'm not sure of a lot of what I'm saying, it's just an explanation.
Possible causes: DLLs and EXE main programs are not created using the same heap.
Workaround:
1. The principle of who assigns who to release;
2. Bypass new and delete, using Glovalalloc and GlobalFree;
3. Change the project options, release version certainly will not have this failure, this will only exist in the debug state, but release will have a memory leak. Change debug under DLL and EXE runtime for dynamic compilation i.e.: Multi-threaded Debug DLL. Because the Multi-thread debug DLL is compiled by the runtime, the compiler allocates the heap for all DLLs that are shared. This will not have multiple release procedures, and there will be no problem.
_CrtIsValidHeapPointer (Puserdata)