_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将被预处理器移除。大概就是这个样子,上面所说的很多东西我都不确定,只是现在的一种解释。

可能原因:DLL和EXE主程序使用的不是同一个堆造成。 

解决办法: 

1. 采用谁分配谁释放的原则; 

2. 绕过 new 和 delete,使用 GlovalAlloc 和 GlobalFree; 

3. 更改工程选项, release 版本肯定不会出现这个失败,这个只会存在 debug 状态下,但是 release 会出现内存泄漏. 更改 debug 下 dll 和 exe 运行库为动态编译即: multi-threaded debug dll. 因为 multi-thread debug dll 运行库编译使编译器为所有dll共享分配的堆。这样就不会存在多个释放过程,也就不会出现问题了.

posted @ 2016-02-17 18:04  QQ76211822  阅读(405)  评论(0编辑  收藏  举报