I am having a problem with the pocket pc 2003 and a nasty data misalignment error. I have narrowed the problem down to the following lines of code in my CBitmap class.
The error message is:
First-chance exception in myGame.exe: 0x80000002: Datatype Misalignment.
Here is my LoadDIB class first. The errors keep occurring with malloc and realloc.
first my declaration in .h
LPBYTE lpDIB;
now the LoadDIB function:
//////////////////////////////////////////////////////////////////////
// CBitmap::LoadDIB
//
// Reads a bitmap from disk and converts it to a DIB buffer.
//
BOOL CBitmap::LoadDIB(LPCTSTR szFileName)
{
HANDLE hFile;
BITMAPFILEHEADER bfh;
LPBYTE lpTemp = NULL;
WORD wPaletteSize = 0;
DWORD dwBitsSize = 0;
BOOL bRet;
//open the file
hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
return FALSE;
}
//read the header
bRet = ReadFile(hFile, &bfh;, sizeof(BITMAPFILEHEADER), &dwImageSize;, NULL);
if ((bRet == FALSE) || (dwImageSize != sizeof(BITMAPFILEHEADER)))
{
CloseHandle(hFile);
return FALSE;
}
//is this a valid bitmap file?
if ((bfh.bfType != 0x4d42) || (bfh.bfReserved1 != 0) || (bfh.bfReserved2 != 0))
{
CloseHandle(hFile);
return FALSE;
}
//allocate memory for the bitmap data
lpDIB = (LPBYTE) malloc(sizeof(BITMAPINFO));
if (lpDIB == NULL)
{
CloseHandle(hFile);
return FALSE;
}
//read in the BITMAPINFOHEADER
bRet = ReadFile(hFile, lpDIB, sizeof(BITMAPINFOHEADER), &dwImageSize;, NULL);
if ((bRet == FALSE) || (dwImageSize != sizeof(BITMAPINFOHEADER)))
{
CloseHandle(hFile);
free(lpDIB);
return FALSE;
}
if (((LPBITMAPINFOHEADER)lpDIB)->biSize != sizeof(BITMAPINFOHEADER))
{
CloseHandle(hFile);
free(lpDIB);
return FALSE;
}
wPaletteSize = PaletteSize((LPSTR)lpDIB);
dwBitsSize = ImageHeight() * BytesPerLine((LPBITMAPINFOHEADER) lpDIB);
//realloc the DIB buffer
lpTemp = (LPBYTE) realloc(lpDIB, sizeof(BITMAPINFOHEADER) + wPaletteSize + dwBitsSize);
if (lpTemp == NULL)
{
CloseHandle(hFile);
free(lpDIB);
return FALSE;
}
lpDIB = lpTemp;
//read palette if one is present
if (wPaletteSize > 0)
{
bRet = ReadFile(hFile, ((LPBITMAPINFO)lpDIB)->bmiColors, wPaletteSize, &dwImageSize;, NULL);
if ((bRet == FALSE) || (dwImageSize != wPaletteSize))
{
CloseHandle(hFile);
free(lpDIB);
return FALSE;
}
}
if (bfh.bfOffBits > 0)
{
if (SetFilePointer(hFile, bfh.bfOffBits, NULL, FILE_BEGIN) == 0xffffffff)
{
CloseHandle(hFile);
free(lpDIB);
return FALSE;
}
}
//read the image data
bRet = ReadFile(hFile, BitmapDataIndex((LPSTR)lpDIB), dwBitsSize, &dwImageSize;, NULL);
if ((bRet == FALSE) || (dwImageSize != dwBitsSize))
{
CloseHandle(hFile);
free(lpDIB);
return FALSE;
}
//close file and return
CloseHandle(hFile);
return TRUE;
}
lastly, the destructor for the CBitmap class:
//////////////////////////////////////////////////////////////////////
// CBitmap Destructor
//
// This function is called when the class is terminated.
//
CBitmap::~CBitmap()
{
if (lpDIB != NULL)
free(lpDIB);
if (hSourceDC != NULL)
{
SelectObject(hSourceDC, hOldBitmap);
DeleteObject(hOldBitmap);
DeleteDC(hSourceDC);
}
}
The problem occurs EVERY time if I try freeing the memory set to lpDIB. If I comment out the
free(lpDIB).
Then the problem doesn't follow, but I have a memory leak. I've stumped on this one. Any help would be greatly appreciated.
The error message is:
First-chance exception in myGame.exe: 0x80000002: Datatype Misalignment.
Here is my LoadDIB class first. The errors keep occurring with malloc and realloc.
first my declaration in .h
LPBYTE lpDIB;
now the LoadDIB function:
//////////////////////////////////////////////////////////////////////
// CBitmap::LoadDIB
//
// Reads a bitmap from disk and converts it to a DIB buffer.
//
BOOL CBitmap::LoadDIB(LPCTSTR szFileName)
{
HANDLE hFile;
BITMAPFILEHEADER bfh;
LPBYTE lpTemp = NULL;
WORD wPaletteSize = 0;
DWORD dwBitsSize = 0;
BOOL bRet;
//open the file
hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
return FALSE;
}
//read the header
bRet = ReadFile(hFile, &bfh;, sizeof(BITMAPFILEHEADER), &dwImageSize;, NULL);
if ((bRet == FALSE) || (dwImageSize != sizeof(BITMAPFILEHEADER)))
{
CloseHandle(hFile);
return FALSE;
}
//is this a valid bitmap file?
if ((bfh.bfType != 0x4d42) || (bfh.bfReserved1 != 0) || (bfh.bfReserved2 != 0))
{
CloseHandle(hFile);
return FALSE;
}
//allocate memory for the bitmap data
lpDIB = (LPBYTE) malloc(sizeof(BITMAPINFO));
if (lpDIB == NULL)
{
CloseHandle(hFile);
return FALSE;
}
//read in the BITMAPINFOHEADER
bRet = ReadFile(hFile, lpDIB, sizeof(BITMAPINFOHEADER), &dwImageSize;, NULL);
if ((bRet == FALSE) || (dwImageSize != sizeof(BITMAPINFOHEADER)))
{
CloseHandle(hFile);
free(lpDIB);
return FALSE;
}
if (((LPBITMAPINFOHEADER)lpDIB)->biSize != sizeof(BITMAPINFOHEADER))
{
CloseHandle(hFile);
free(lpDIB);
return FALSE;
}
wPaletteSize = PaletteSize((LPSTR)lpDIB);
dwBitsSize = ImageHeight() * BytesPerLine((LPBITMAPINFOHEADER) lpDIB);
//realloc the DIB buffer
lpTemp = (LPBYTE) realloc(lpDIB, sizeof(BITMAPINFOHEADER) + wPaletteSize + dwBitsSize);
if (lpTemp == NULL)
{
CloseHandle(hFile);
free(lpDIB);
return FALSE;
}
lpDIB = lpTemp;
//read palette if one is present
if (wPaletteSize > 0)
{
bRet = ReadFile(hFile, ((LPBITMAPINFO)lpDIB)->bmiColors, wPaletteSize, &dwImageSize;, NULL);
if ((bRet == FALSE) || (dwImageSize != wPaletteSize))
{
CloseHandle(hFile);
free(lpDIB);
return FALSE;
}
}
if (bfh.bfOffBits > 0)
{
if (SetFilePointer(hFile, bfh.bfOffBits, NULL, FILE_BEGIN) == 0xffffffff)
{
CloseHandle(hFile);
free(lpDIB);
return FALSE;
}
}
//read the image data
bRet = ReadFile(hFile, BitmapDataIndex((LPSTR)lpDIB), dwBitsSize, &dwImageSize;, NULL);
if ((bRet == FALSE) || (dwImageSize != dwBitsSize))
{
CloseHandle(hFile);
free(lpDIB);
return FALSE;
}
//close file and return
CloseHandle(hFile);
return TRUE;
}
lastly, the destructor for the CBitmap class:
//////////////////////////////////////////////////////////////////////
// CBitmap Destructor
//
// This function is called when the class is terminated.
//
CBitmap::~CBitmap()
{
if (lpDIB != NULL)
free(lpDIB);
if (hSourceDC != NULL)
{
SelectObject(hSourceDC, hOldBitmap);
DeleteObject(hOldBitmap);
DeleteDC(hSourceDC);
}
}
The problem occurs EVERY time if I try freeing the memory set to lpDIB. If I comment out the
free(lpDIB).
Then the problem doesn't follow, but I have a memory leak. I've stumped on this one. Any help would be greatly appreciated.