Sign in to follow this  

Crash with the delete command.

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

This topic is 4732 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this