Jump to content
  • Advertisement
Sign in to follow this  
disanti

Crash with the delete command.

This topic is 5043 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

In C++, I'm having a problem with the delete command crashing my game. I will just get down to the code and tell you what line crashes, I've been debugging for the past 2 hours, fixed about 5 bugs, etc... This crash is creating a huge memory leak. The allocated memory that is crashing when deleted is: pixeldata
u32 tld_texturemanager::loadTGA(const char* filename)
{
	u32 imageid = 0;
	tga_header header;
	u8* pixeldata = NULL;

	/* Load the file */
	ifstream tgafile;
	tgafile.open(filename, ifstream::in|ifstream::binary);
	if(!tgafile.is_open())
	{
		tld_Message("Failed to load image: %s\n", filename);
		tld_Message("\tSubstituting with default texture.\n");
		return 0;
	}

	tgafile.read((char*)&header, sizeof(tga_header));
	
	/* Test the file header */
	if(header.id_length != 0x00 || header.colormaptype != 0x00 || header.bpp != 0x18)
	{
		tld_Message("Incorrect image type: %s\n", filename);
		tld_Message("\tSubstituting with default texture.\n");
		return 0;
	}

	/* Read the pixel data */
	pixeldata = new u8[header.width*header.height*3];
	if(header.imagetype == 0x02) //no compression rgb
	{
		for(int x = 0; x < header.width*header.height; x++)
		{
			// read and reverse the pixel data
			u8 r, g, b;
			b = tgafile.peek();
			g = tgafile.peek();
			r = tgafile.peek();
			pixeldata[x*3]	 = r;
			pixeldata[x*3+1] = g;
			pixeldata[x*3+3] = b;
		}
	}
	else if(header.imagetype == 0x0A) //rle compressed rgb
	{
		u8 r, g, b, packet;
		int position = 0;
		packet = tgafile.peek();

		while(position < header.width*header.height)
		{
			if((packet & 0x80) == 0x80) //rle packet
			{
				int count = (packet & 0x7F) + 1;

				b = tgafile.peek();
				g = tgafile.peek();
				r = tgafile.peek();

				for(int i = 0; i < count; i++)
				{
					pixeldata[(position+i)*3] = r;
					pixeldata[(position+i)*3+1] = g;
					pixeldata[(position+i)*3+2] = b;
					position++;
				}
			}
			else if((packet & 0x80) == 0x00) //raw packet
			{
				int count = (packet & 0x7F) + 1;

				for(int i = 0; i < count; i++)
				{
					b = tgafile.peek();
					g = tgafile.peek();
					r = tgafile.peek();
					pixeldata[(position+i)*3] = r;
					pixeldata[(position+i)*3+1] = g;
					pixeldata[(position+i)*3+2] = b;
					position++;
				}
			}
		}
	}

	//feed it into opengl
	glGenTextures(1, &imageid);
	glBindTexture(GL_TEXTURE_2D, imageid);
	glTexImage2D(GL_TEXTURE_2D, 0, 3, header.width, header.height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixeldata);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, header.width, header.height, GL_RGB, GL_UNSIGNED_BYTE, pixeldata);

	//cleanup
	if(pixeldata)
	{
		delete pixeldata;
		pixeldata = NULL;
	}

	tld_Message("Successfully loaded Targa: %s\n", filename);
	return imageid;
}



EDIT: u8 is declared as unsigned char. The leak I get from the pixeldata not being released is 199680 bytes long for a 256x256x24 texture file. I know, my code looks a bit messy, I should probably clean it up. I just really want to fix this crash, it is getting to me. I've never had this problem before. Thanks in advance~ John DiSanti

Share this post


Link to post
Share on other sites
Advertisement
Hi!

The variable pixeldata is an array an so you should free its memory like this:

delete [] pixeldata;

This should fix your memory leak.

Bye, hunta

Share this post


Link to post
Share on other sites
Also looks like you are overwriting the array bounds, header.width*header.height*3 is the max index you can have on pixeldata, but in your loop you are going up to header.width*header.height*3.

EDIT: I am talking about pixeldata[x*3+3] = b, it should be pixeldata[x*3+2] = b;

Share this post


Link to post
Share on other sites
Quote:
Original post by hunta
delete [] pixeldata;


Indeed. You must allways use the appropriate memory free-er:

new[] <--> delete[]
new <--> delete
malloc <--> free

Mixing them up is extreemely bad mojo, prone to crashing.

Share this post


Link to post
Share on other sites
Thanks a lot guys, especially Jingo, I didn't even notice that! ;)
Using delete [] pixeldata fixed the problem, I got to look out for those.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!