Expression: _CrtIsValidHeapPointer(pUserData)

程序遇到如题的运行时报错,参考下面这段文字,采取将自定义类的对象定义改为new方式生成后问题解决。

!!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());
}

问题就出在红色的地方,自定义了一个类

将上面的语句改为

 ColHistogram * pMyColHist;
 pMyColHist = new ColHistogram;

就可以了,不过现在也不知道为什么

(MSDN)中的这段话

The   _CrtIsValidHeapPointer   function   is   used   to   ensure   that   a   specific   memory   address   is   within   the   local   heap.   The   “local”   heap   refers   to   the   heap   created   and   managed   by   a   particular   instance   of   the   C   run-time   library.   If   a   dynamically   linked   library   (DLL)   contains   a   static   link   to   the   run-time   library,   then   it   has   its   own   instance   of   the   run-time   heap,   and   therefore   its   own   heap,   independent   of   the   application’s   local   heap.   When   _DEBUG   is   not   defined,   calls   to   _CrtIsValidHeapPointer   are   removed   during   preprocessing.

看了这段话稍微觉得有点意思了,我在程序中自己申请了本地堆,也有要生成动态连接库的DIB类,要连接c运行库,那么我的ColHistogram的实例必须动态生成,因为它在c运行库中没有对应的堆。比如我添加Cstring str;程序就不会有问题,但是我只知道CString是系统定义的,和c运行库有什么关系我就不清楚了。如果静态链接C运行库,那么,dll就要拥有一个独立于应用程序(调用它的exe)的本地堆(但是我的程序没有),如果没有定义_DEBUG,那么_CrtIsValidHeapPointer将被预处理器移除。大概就是这个样子,上面所说的很多东西我都不确定,只是现在的一种解释。

还有dbgheap.c文件似乎是在dll里,还没有办法看

转自:http://blog.csdn.net/haokongdashi/archive/2010/02/23/5318461.aspx

原文地址:https://www.cnblogs.com/sifenkesi/p/1995066.html