Bitmap problem

Started by
0 comments, last by BigMac 19 years, 11 months ago
Hi, I have simple CTexture class which I use to load bitmaps. Everything works fine untill I want to delete CTexure object. Then program crash with "DAMAGE: after Normal Block..." message. The application only crashes in debug mode, in release mode it works fine. (I use VS 6.0) I have tried to load two different bitamps: First bitmap: Resoltution 256x256 the app crashes Second bitmap: Resolution 562x168 the app works fine Load bitmap function:

unsigned char* CTexture::LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
	FILE *filePtr;								
	
	unsigned int		imageIdx = 0;			
	unsigned char		tempRGB;
	unsigned char*		imageData;
	
	filePtr = fopen(filename, "rb");
	
	if (filePtr == NULL)
		return NULL;

	
	fread(&m_BitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
	
	
	if (m_BitmapFileHeader.bfType != BITMAP_ID)
	{
		fclose(filePtr);
		return NULL;
	}

	
	fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);

	
	fseek(filePtr, m_BitmapFileHeader.bfOffBits, SEEK_SET);

	
	imageData = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
	
	
	if (!imageData)
	{
		free(imageData);
		fclose(filePtr);
		return NULL;
	}

	
	fread(imageData, 1, bitmapInfoHeader->biSizeImage, filePtr);

	
	if (imageData == NULL)
	{
		fclose(filePtr);
		return NULL;
	}

	
	for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3)
	{
		tempRGB = imageData[imageIdx];
		imageData[imageIdx] = imageData[imageIdx + 2];
		imageData[imageIdx + 2] = tempRGB;
	}

	
	fclose(filePtr);
	

	return imageData;
}

//Setup method

bool CTexture::SetupTexture(char* filename)
{
 m_pBitmapData = LoadBitmapFile(filename, &m_BitmapInfoHeader);
 
 //I deleted whole opengl code but it still dosen't work


}
Destructor:

CTexture::~CTexture()
{
 if(m_pBitmapData != NULL)
    free(m_pBitmapData);
  
  m_pBitmapData = NULL;
}
 
Thanks in advance for help. [edited by - BigMac on May 21, 2004 6:56:45 PM]
Advertisement
At a guess...

for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3){		tempRGB = imageData[imageIdx];imageData[imageIdx] = imageData[imageIdx + 2];imageData[imageIdx + 2] = tempRGB;}


This is going out of bounds. You allocated memory of size bitmapInfoHeader->biSizeImage. The loop above goes all the way up to one less than the memory you have allocated (fine) but then you access +2 which writes to memory past the block, hence "damage after block", you only get it in Debug mode because release mode doesnt have detection like that. But its dead important to correct it, or you may encounter problems in release mode in the future.

Hope this gibberish helps!

[edited by - empirical on May 21, 2004 7:42:07 PM]

This topic is closed to new replies.

Advertisement