1

I'm getting a first-chance exception error when I uncomment a line of code that creates a bitmap. I created this spritesheet BMP in the same manner as other sprites I've made. The error message is this:

First-chance exception at 0x004788ca in HenwayRevamped.exe: 0xC0000005: Access violation writing location 0x02224000.

I'm running a game built on Michael Morrison's GameEngine class. I've done a few hours of research on this error, including many StackOverflow threads & this handy site but am still not sure what the problem is. Here's the function call:

// Create a bitmap from a resource
Bitmap::Bitmap(HDC hDC, UINT uiResID, HINSTANCE hInstance)
  : m_hBitmap(NULL), m_iWidth(0), m_iHeight(0)
{
  Create(hDC, uiResID, hInstance);
}

Here's the function:

BOOL Bitmap::Create(HDC hDC, UINT uiResID, HINSTANCE hInstance)
{
  // Free any previous DIB info
  Free();

  // Find the bitmap resource
  HRSRC hResInfo = FindResource(hInstance, MAKEINTRESOURCE(uiResID), RT_BITMAP);
  if (hResInfo == NULL)
    return FALSE;

  // Load the bitmap resource
  HGLOBAL hMemBitmap = LoadResource(hInstance, hResInfo);
  if (hMemBitmap == NULL)
    return FALSE;

  // Lock the resource and access the entire bitmap image
  PBYTE pBitmapImage = (BYTE*)LockResource(hMemBitmap);
  if (pBitmapImage == NULL)
  {
    FreeResource(hMemBitmap);
    return FALSE;
  }

  // Store the width and height of the bitmap
  BITMAPINFO* pBitmapInfo = (BITMAPINFO*)pBitmapImage;
  m_iWidth = (int)pBitmapInfo->bmiHeader.biWidth;
  m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight;

  // Get a handle to the bitmap and copy the image bits
  PBYTE pBitmapBits;
  m_hBitmap = CreateDIBSection(hDC, pBitmapInfo, DIB_RGB_COLORS,
    (PVOID*)&pBitmapBits, NULL, 0);
  if ((m_hBitmap != NULL) && (pBitmapBits != NULL))
  {
    const PBYTE pTempBits = pBitmapImage + pBitmapInfo->bmiHeader.biSize +
      pBitmapInfo->bmiHeader.biClrUsed * sizeof(RGBQUAD);


    CopyMemory(pBitmapBits, pTempBits, pBitmapInfo->bmiHeader.biSizeImage);

    // Unlock and free the bitmap graphics object
    UnlockResource(hMemBitmap);
    FreeResource(hMemBitmap);
    return TRUE;
  }

  // Something went wrong, so cleanup everything
  UnlockResource(hMemBitmap);
  FreeResource(hMemBitmap);
  Free();
  return FALSE;
}

Also, here's a screenshot of my IDE w/ Locals window. The green arrow icon in the gutter points to the line where the exception is thrown:

Image Hosted by ImageShack.us http://imageshack.us/a/img341/7678/20121216010703am.png

Is it my device context that's the problem? The red exclamation icon under the hDC variable looks like it might be pointing to the problem, but it has a handle assigned, so I'm not sure. I've run out of ideas, and am turning to the masters.

KongMD
  • 171
  • 1
  • 4
  • 14
  • You should likely already know that this is not the same `Create()` call being fired out of the constructor you list first in your post. That code fires `Create()` expecting a device context, width, height, and color. The unrelated `Create()` you're showing is loading a resource bitmap (or at least attempting to). – WhozCraig Dec 16 '12 at 07:39
  • What's inside `pBitmapInfo->bmiHeader`? I'd expect `biSizeImage` value to be wrong there. – Roman R. Dec 16 '12 at 10:55
  • Thanks for the heads-up, @WhozCraig; I posted the wrong overloaded call in error. The actually call is now in the OP. @Roman, that call returns 147458. From my Windows properties window for the image, the image size is 147512. If that's the issue, how can I resolve it? – KongMD Dec 16 '12 at 14:02
  • 3
    What I see is that you have 768x64 image. I don't see its bitness, let's suppose it is 24 bits/pixel. Hence, 147456 bytes in total. That's what should be in `biSizeImage`. I would expect the API to allocate internal buffer of this length. If you have a wrong value there (which the API might have ignored), then are trying to copy more bytes then the buffer can hold and hence the expection. This is why I assumed this value might be wrong. – Roman R. Dec 16 '12 at 14:10

3 Answers3

2

The access violation occurs on write, which suggests that you're overrunning the destination buffer. There are many different types of bitmap formats, so it can be difficult computing the right sizes for the buffers. Your code does work with some bitmaps, so I suspect that you've got a bad assumption about bit depth, color table size, alignment, or something along those lines.

Whenever you can, you may as well let Windows do the work:

    BOOL Bitmap::Create2(UINT uiResID, HINSTANCE hInstance) {
        BOOL bSuccess = FALSE;
        Free();
        m_hBitmap = reinterpret_cast<HBITMAP>(
            ::LoadImage(hInstance, MAKEINTRESOURCE(uiResID), IMAGE_BITMAP,
                        0, 0, LR_CREATEDIBSECTION));
        if (m_hBitmap != NULL) {
            BITMAP bmp;
            if (::GetObject(m_hBitmap, sizeof(bmp), &bmp) == sizeof(bmp)) {
                m_iWidth = bmp.bmWidth;
                m_iHeight = bmp.bmHeight;
                bSuccess = TRUE;
            }
        }
        return bSuccess;
    }

This should work for all valid bitmaps. Note that you no longer need to pass in a handle to a DC.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
  • That worked beautifully, and is much more concise. Thanks for improving upon my hacked-together solution that would've likely failed in the future! – KongMD Dec 18 '12 at 02:53
0

A first chance exception isn't usually something to worry about, and the debugger shouldn't stop on them unless you've changed some settings. An unhandled exception is an actual problem, where there was an exception the application did not catch and handle.

If the debugger is breaking when it happens, you can change the exception settings to prevent that in the future, or you can just continue and see if it turns into an unhandled exception.

Some more information about first chance versus second chance here: http://support.microsoft.com/kb/105675

Retired Ninja
  • 4,785
  • 3
  • 25
  • 35
  • Actually, it was an unhandled exception originally that was breaking functionality in the game. I first came across the issue as described in [this thread](http://stackoverflow.com/questions/13895604/win32-bitmap-instantiation-breaking-function), and explicitly ticked the "Win32 Exceptions" checkbox because of a bug with the wow64 emulation later. – KongMD Dec 16 '12 at 13:12
-1

Thanks to Roman's insight, I was able to come up with this workaround that compiles & works normally. If I get more exceptions with other images, I'll make an array and iterate through it, but this works for now:

/*  This causes strange black lines at the top of some sprites. Pixel bleeding?
    if(pBitmapInfo->bmiHeader.biSizeImage > neededBytes )
        CopyMemory(pBitmapBits, pTempBits, neededBytes);
    else
        CopyMemory(pBitmapBits, pTempBits, pBitmapInfo->bmiHeader.biSizeImage);
    */

    //workaround for powerup sprite
    if(pBitmapInfo->bmiHeader.biSizeImage == 294914 )
        CopyMemory(pBitmapBits, pTempBits, neededBytes);
    else
        CopyMemory(pBitmapBits, pTempBits, pBitmapInfo->bmiHeader.biSizeImage);
KongMD
  • 171
  • 1
  • 4
  • 14