关于C ++ :.访问冲突读取位置

.Access violation reading location

我遇到了一个非常奇怪的问题。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
::boost::shared_ptr<CQImageFileInfo> pInfo=CQUserViewDataManager::GetInstance()->GetImageFileInfo(nIndex);
Image* pImage=pInfo->m_pThumbnail;
if(pImage==NULL)
    pImage=m_pStretchedDefaultThumbImage;
else
{
    //
    int sourceWidth  = pInfo->GetWidth();
    int sourceHeight = pInfo->GetHeight();

    int destX = 0,
        destY = 0;

    float nPercent  = 0;
    float nPercentW = ((float)GetThumbImageWidth()/(float)sourceWidth);;
    float nPercentH = ((float)GetThumbImageHeight()/(float)sourceHeight);

    if(nPercentH < nPercentW)
    {
        nPercent = nPercentH;
        destX    = (int)((GetThumbImageWidth() - (sourceWidth * nPercent))/2);
    }
    else
    {
        nPercent = nPercentW;
        destY    = (int)((GetThumbImageHeight() - (sourceHeight * nPercent))/2);
    }

    int destWidth  = (int)(sourceWidth * nPercent);
    int destHeight = (int)(sourceHeight * nPercent);
    rcShowImage=CRect(rc.left+destX, rc.top+destY,rc.left+destX+destWidth,rc.top+destY+destHeight);
}
ASSERT(pImage != NULL); // passed assertion...
graphics.DrawImage(pImage,rcShowImage.left,rcShowImage.top,
rcShowImage.Width(),rcShowImage.Height()); // problem happened here.

我收到以下异常:

1
2
First-chance exception at 0x004095b0 in ec.exe: 0xC0000005: Access violation reading location 0xfeeefef2.
Unhandled exception at 0x004095b0 in ec.exe: 0xC0000005: Access violation reading location 0xfeeefef2.

我已经检查了pImage,我确定在调用graphics.DrawImage时,它不是NULL

  • 为什么会发生这样的问题?
  • 什么是0xfeeefef2

0xfeeefeee是Windows堆(不是C运行时堆)的调试版本用于未初始化内存的填充模式。 0xfeeefef20xfeeefeee+4。 听起来您正在取消引用位于堆(从堆分配的内存)中(或从中复制)的未初始化指针。

当您在调试器中启动程序时,调试堆会自动启用,这与使用调试器附加到已在运行的程序相反。

Mario Hewardt和Daniel Pravat所著的《高级Windows调试》一书中有一些有关Windows堆的不错的信息,事实证明,有关堆的章节已在网站上作为示例章节出现。


当你做

1
pImage=m_pStretchedDefaultThumbImage;

m_pStretchedDefaultThumbImage是否可能未初始化?


如果您在第三行粘贴pImage == NULL,会发生什么? 在这种情况下,不会为rcShowImage分配值。