编译通过 再debug下运行时出现 Expression: _CrtIsValidHeapPointer(pUserData)的问题,调到release下顺利运行,图片显示正常。
原因现在还搞不太清楚,查了下别人在这方面的解释
一,http://blog.163.com/yangqingkun816@126/blog/static/12215451620099892921608/
检查指针有效性
下面的示例使用 _CrtIsValidPointer 验证给定的内存范围对于读或写是否有效。
_ASSERTE(_CrtIsValidPointer( address, size, TRUE );
下面的示例使用 _CrtIsValidHeapPointer 验证指针指向本地堆(由 C 运行时库的这个实例创建和管理的堆;DLL 可以有它自己的库实例,因而也可以有它自己的、位于应用程序堆之外的堆)中的内存。该断言不仅捕捉空地址或超出边界的地址,还捕捉指向静态变量、堆栈变量和其他任何非本地内存的指针。
_ASSERTE(_CrtIsValidPointer( myData );
========
//从CSDN.NET
_CrtIsValidHeapPointer(pUserData)的问题:
_CrtIsValidHeapPointer确认内存地址在本地堆。……如果静态链接C运行库,那么,dll拥有一个独立于应用程序(调用它的 exe)的本地堆。(所以你上面的程序会Debug Assertion Failed),如果没有定义_DEBUG,那么_CrtIsValidHeapPointer将被预处理器移除。
如果是:Debug Assertion Failed! 解决的方法有二:
1、动态链接C运行库:
2、设置统一的Debug/Release版本(比如全部设置为release版本);(ok)
如果“Assertion Failed”的话,估计程序是会出问题的.
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.
二,http://richardchenblog.blog.sohu.com/32362251.html
!!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里,还没有办法看