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.
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:
This should work for all valid bitmaps. Note that you no longer need to pass in a handle to a DC.