Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

BigMac

Bitmap problem

This topic is 5239 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!