views:

590

answers:

3

I've met a really strange problem: The code is as follow:

::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.

I have checked the pImage, I am sure when graphics.DrawImage is called, it is not NULL, why such a problem happened? What is 0xfeeefef2?

Thanks in advance!

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.

A: 

What happens if pImage == NULL on the third line you pasted? In that case, rcShowImage is not assigned a value.

Greg Hewgill
It looks like rcShowImage is on the stack (members accessed with "."), so accessing it shouldn't be a problem.
Andrew Medico
True, but we don't know what type it is and whether the constructor does anything sensible.
Greg Hewgill
+2  A: 

When you do

pImage=m_pStretchedDefaultThumbImage;

Is there a possibility that m_pStretchedDefaultThumbImage is uninitialized?

Martin Cote
+6  A: 

0xfeeefeee is a fill pattern that the debug version of the Windows heap (not the C runtime heap) uses for uninitialized memory. 0xfeeefef2 is 0xfeeefeee+4. It sounds like you're dereferencing an uninitialized pointer located in (or copied from) a block of memory allocated from the heap.

The debug heap automatically gets enabled when you start your program in the debugger, as opposed to attaching to an already-running program with the debugger.

The book Advanced Windows Debugging by Mario Hewardt and Daniel Pravat has some decent information about the Windows heap, and it turns out that the chapter on heaps is up on the web site as a sample chapter.

bk1e