Memory leak on Pocket PC

I am running through the tutorials on http://www.zeuscmd.com and I’ve noticed something going on that is really, really wrong. I modified the “textured mapping” tutorial (#17) to load 5 separate textures and bind a particular one to the floating cube whenever a D-pad left or right is pressed (basically treating the texture array like a stack…Left moves up the stack…Right moves down the stack) (I also have transparency enabled for the project as well as a single light source) …anyway, I played around with the program for a few minutes and then, whenever I tried to kill the program, my device completely locked up. I read a previous thread where someone claimed to be having the same problem with their device locking up, and that “ugDestroyWindow(uwin)” should be used to destroy the program rather than “exit(0)” , but neither way keeps my program from consistently eating up memory (about 150KB/sec) until the device completely crashes. Is there a problem with the Vincent Library that might be causing this? I’m including my code, just in case anyone is interested. (If you want to try the program, you’ll have to load your own bitmaps)

//Kip Bates
//OpenGL ES
//October 27, 2005

#pragma comment(lib, "libGLES_CM.lib")
#pragma comment(lib, "ug.lib")

#include "ug.h"


GLuint textures[5];




float			xrot = 0.0;
float			yrot = 0.0;

int				mWidth = 0;
int				mHeight = 0;

int				texIdx = 1;



GLfloat box[] = {
	//FRONT FACE
		-0.5f, -0.5f, 0.5f,
		 0.5f, -0.5f, 0.5f,
		-0.5f,  0.5f, 0.5f,
		 0.5f,  0.5f, 0.5f,
	//BACK FACE
		-0.5f, -0.5f, -0.5f,
		-0.5f,  0.5f, -0.5f,
		 0.5f, -0.5f, -0.5f,
		 0.5f,  0.5f, -0.5f,
	//LEFT FACE
		-0.5f, -0.5f,  0.5f,
		-0.5f,  0.5f,  0.5f,
		-0.5f, -0.5f, -0.5f,
		-0.5f,  0.5f, -0.5f,
	//RIGHT FACE
		 0.5f, -0.5f, -0.5f,
		 0.5f,  0.5f, -0.5f,
		 0.5f, -0.5f,  0.5f,
		 0.5f,  0.5f,  0.5f,
	//TOP FACE
		-0.5f,  0.5f,  0.5f,
		 0.5f,	0.5f,  0.5f,
		-0.5f,	0.5f, -0.5f,
		 0.5f,  0.5f, -0.5f,
	//BOTTOM FACE
		-0.5f, -0.5f,  0.5f,
		-0.5f, -0.5f, -0.5f,
		 0.5f, -0.5f,  0.5f,
		 0.5f, -0.5f, -0.5f
		 
};


GLfloat texCoords[] = {
		//FRONT
		0.0f, 0.0f,
		1.0f, 0.0f,
		0.0f, 1.0f,
		1.0f, 1.0f,
		//BACK
		1.0f, 0.0f,
		1.0f, 1.0f,
		0.0f, 0.0f,
		0.0f, 1.0f,
		//LEFT
		1.0f, 0.0f,
		1.0f, 1.0f,
		0.0f, 0.0f,
		0.0f, 1.0f,
		//RIGHT
		1.0f, 0.0f,
		1.0f, 1.0f,
		0.0f, 0.0f,
		0.0f, 1.0f,
		//TOP
		0.0f, 0.0f,
		1.0f, 0.0f,
		0.0f, 1.0f,
		1.0f, 1.0f,
		//BOTTOM
		1.0f, 0.0f,
		1.0f, 1.0f,
		0.0f, 0.0f,
		0.0f, 1.0f

};


float lightAmbient[]	= {0.8f, 0.8f, 0.8f, 1.0f};


float lightDiffuse[]	= {0.5f, 0.0f, 0.0f, 1.0f};
float lightSpecular[]	= {1.0f, 1.0f, 1.0f, 1.0f};

float matAmbient[]		= {0.8f, 0.8f, 0.8f, 0.8f};

float matDiffuse[]		= {1.0f, 1.0f, 1.0f, 1.0f};
float matSpecular[]		= {1.0f, 1.0f, 1.0f, 1.0f};



//spotlight stuff.  Not used in this example
float lightPosition[]	= {2.0f, 2.0f, 3.0f, 0.0f};
float lightDirection[]	= {-2.0f, -2.0f, -3.0f};


//forward declarations
bool init();
unsigned char *loadBitmap(char, BITMAPINFOHEADER);
void display(UGWindow);
void reshape(UGWindow, int, int);
void keyboard(UGWindow, int, int, int);
void reshape(UGWindow, int, int);
void pointer(UGWindow, int, int, int, int);
void idle(UGWindow);
bool LoadTextures();






//CPP Helper functions

unsigned char *loadBmp(char *filename, BITMAPINFOHEADER *bmpInfo)
{
	FILE *fileptr;

	BITMAPFILEHEADER bmpFile;
	unsigned char *bmpImage = NULL;
	unsigned char tempRGB;				//In a Bitmap, color is stored in BGR...need a way to flip it to RGB

	TCHAR path[256];
	char fullPath[256];
	GetModuleFileName(NULL, path, 256);	//returns full path with .exe name

	
	TCHAR *pos = wcsrchr(path, '\\');	//gets the last occurrance of '\'
	*(pos + 1) = '\0';
	
	wcstombs(fullPath, path, 256);

	strcat(fullPath, filename);

	//*** We now have the location of the bitmap.  Open the file in read binary mode ***
	fileptr = fopen(fullPath, "rb");

	if(!fileptr)
	{
		MessageBox(NULL, L"Can't Find Bitmap", L"Error", MB_OK);
		return NULL;
	}

	//Read in the BitmapFileHeader
	fread(&bmpFile, sizeof(BITMAPFILEHEADER), 1, fileptr);

	if(bmpFile.bfType != 0x4D42)
	{
		MessageBox(NULL, L"Incorrect Texture Type", L"Error", MB_OK);
		fclose(fileptr);
		return NULL;
	}

	//Read in the BitmapInfoHeader
	fread(bmpInfo, sizeof(BITMAPINFOHEADER), 1, fileptr); 

	//move file pointer from beginning of file to beginning of image data
	fseek(fileptr, bmpFile.bfOffBits, SEEK_SET);
	//allocate memory
	bmpImage = new unsigned char[bmpInfo->biSizeImage];

	//out of resources
	//couldn't create bmpImage
	if(!bmpImage)
	{
		MessageBox(NULL, L"Out of Memory", L"Error", MB_OK);
		delete[] bmpImage;
		fclose(fileptr);
		return NULL;
	}


	//Load the data from "file" 1 bit at a time into "bmpImage" for every bit up to
	//"bmpInfo->biSizeImage"
	fread(bmpImage, 1, bmpInfo->biSizeImage, fileptr);
	
	//error reading from "file"
	if(!bmpImage)
	{
		MessageBox(NULL, L"Error reading bitmap", L"Error", MB_OK);
		fclose(fileptr);
		return NULL;
	}

	
	//reformats bitmap info for RGB
	for(unsigned int i = 0; i < bmpInfo->biSizeImage; i+=3)
	{
		tempRGB = bmpImage[i];
		bmpImage[i] = bmpImage[i+2];
		bmpImage[i+2] = tempRGB;
	}
	fclose(fileptr);

	//returns properly formatted data
	return bmpImage;


}




















//OpenGL stuff


bool init()
{
	
	
	if(!LoadTextures())
	{
		MessageBox(NULL, L"Error loading texture", L"Error", MB_OK);
		return false;
	}
	
	//enable texturing
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
	glDisable(GL_DEPTH_TEST);	//allows for display of surfaces through transparent forward surface
	

	//enable lighting
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);




	//Set up lights and materials
	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, matAmbient);
		//glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, matDiffuse);
		//glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, matSpecular);
		//glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 20.0f);

	glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
		//glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
		//glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular);

		//glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
		//glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, lightDirection);
		//glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 1.2f);
		//glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 20.0f);


	glDepthFunc(GL_LEQUAL);
	
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClearDepthf(1.0f);

	glVertexPointer(3, GL_FLOAT, 0, box);
	glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);

	//glEnable(GL_CULL_FACE);
	glShadeModel(GL_SMOOTH);

	return true;
	
	
}

void display(UGWindow uwin)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	

	ugluLookAtf(
		0.0f, 0.0f, 3.0f,
		0.0f, 0.0f, 0.0f,
		0.0f, 1.0f, 0.0f);
	glPushMatrix();
		


		glRotatef(xrot, 1.0f, 0.0f, 0.0f);
		glRotatef(yrot, 0.0f, 1.0f, 0.0f);

		if(texIdx == 1)
		{
			glBindTexture(GL_TEXTURE_2D, textures[0]);
		}
		else if(texIdx == 2)
		{
			glBindTexture(GL_TEXTURE_2D, textures[1]);
		}
		else if(texIdx == 3)
		{
			glBindTexture(GL_TEXTURE_2D, textures[2]);
		}
		
		else if(texIdx == 4)
		{
			glBindTexture(GL_TEXTURE_2D, textures[3]);
		}

		else if(texIdx == 5)
		{
			glBindTexture(GL_TEXTURE_2D, textures[4]);
			
		}
				
		else
		{
			glBindTexture(GL_TEXTURE_2D, textures[1]);
		}


		// FRONT AND BACK
	   glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
	   glNormal3f(0.0f, 0.0f, 1.0f);
	   glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
	   glNormal3f(0.0f, 0.0f, -1.0f);
	   glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);

	   // LEFT AND RIGHT
	   glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
	   glNormal3f(1.0f, 0.0f, 0.0f);
	   glDrawArrays(GL_TRIANGLE_STRIP, 8, 4);
	   glNormal3f(-1.0f, 0.0f, 0.0f);
	   glDrawArrays(GL_TRIANGLE_STRIP, 12, 4);

	   // TOP AND BOTTOM
	   glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
	   glNormal3f(0.0f, 1.0f, 0.0f);
	   glDrawArrays(GL_TRIANGLE_STRIP, 16, 4);
	   glNormal3f(0.0f, -1.0f, 0.0f);
	   glDrawArrays(GL_TRIANGLE_STRIP, 20, 4);
		
	glPopMatrix();


	glFlush();
	ugSwapBuffers(uwin);
}

bool LoadTextures()
{
	BITMAPINFOHEADER chromeInfo;
	BITMAPINFOHEADER reptileInfo;
	BITMAPINFOHEADER redwoodInfo;
	BITMAPINFOHEADER zeusInfo;
	BITMAPINFOHEADER waterInfo;



	unsigned char	*reptile	= NULL;
	
	unsigned char	*chrome		= NULL;
	unsigned char	*redwood	= NULL;
	unsigned char	*zeus		= NULL;
	unsigned char	*water		= NULL;


	chrome = loadBmp("chrome.bmp", &chromeInfo);
	reptile = loadBmp("lizard.bmp", &reptileInfo);
	redwood = loadBmp("redwood.bmp", &redwoodInfo);
	zeus = loadBmp("zeus.bmp", &zeusInfo);
	water = loadBmp("water.bmp", &waterInfo);

	if(!chrome || !reptile || !redwood || !zeus || !water)
	{
		return false;
	}




	//**The following set up turns the loaded bitmaps into texture objects.

	//assign names to the textures
	glGenTextures(5, textures);

	//set up chrome texture
	//bind the chrome texture to the first texture name
	glBindTexture(GL_TEXTURE_2D, textures[0]);
	//See notes for specification
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, chromeInfo.biWidth, chromeInfo.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, chrome);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	
	//set up reptile
	//bind the reptile texture to the second texture name
	glBindTexture(GL_TEXTURE_2D, textures[1]);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, reptileInfo.biWidth, reptileInfo.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, reptile);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	//set up redwood
	//bind the redwood texture to the third texture name
	glBindTexture(GL_TEXTURE_2D, textures[2]);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, redwoodInfo.biWidth, redwoodInfo.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, redwood);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	
	
	//set up zeus
	//bind the zeus texture to the fourth texture name
	glBindTexture(GL_TEXTURE_2D, textures[3]);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, zeusInfo.biWidth, zeusInfo.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, zeus);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	//set up water
	//bind the water texture to the fifth texture name
	glBindTexture(GL_TEXTURE_2D, textures[4]);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, waterInfo.biWidth, waterInfo.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, water);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	//bitmaps have been loaded and bound to the scene
	//free bitmap data from memory
	
	delete[] chrome;
	delete[] reptile;
	delete[] redwood;
	delete[] zeus;
	delete[] water;
	return true;
}


















//Windows stuff

void keyboard(UGWindow uwin, int key, int x, int y)
{
	switch(key)
	{

		
	
	
	case 'b' :			{	if(glIsEnabled(GL_BLEND))
							{
								//MessageBox(NULL, L"B pushed", L"Press_Event", MB_OK);
								glDisable(GL_BLEND);
								glEnable(GL_DEPTH_TEST);
							}
							else
							{
								glEnable(GL_BLEND);
								glDisable(GL_DEPTH_TEST);
							}
							break;
						}

	case 'l' :			{	if(glIsEnabled(GL_LIGHTING))
							{
								glDisable(GL_LIGHTING);
							}
							else
							{
								glEnable(GL_LIGHTING);
							}
							break;
						}
	
	case 'm' :			{
							if(glIsEnabled(GL_COLOR_MATERIAL))
							{
								glDisable(GL_COLOR_MATERIAL);
							}
							else
							{
								glEnable(GL_COLOR_MATERIAL);
							}
							break;
						}
	case 'q' :			{exit(0); break;}
						
	case UG_KEY_UP:		{exit(0); break;}
	case UG_KEY_LEFT:	{texIdx--;
							if(texIdx < 1)
							{
								texIdx = 1;
							}
							break;
						}
	case UG_KEY_RIGHT:	{texIdx++;
							if(texIdx > 5)
							{
								texIdx = 5;
							}
							break;
						}

	}
}
void reshape(UGWindow uwin, int width, int height)
{
	mWidth = width;
	mHeight = height;
	if(!height)
	{
		height = 1;
	}
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	glViewport(0,0,width,height);

	
		ugluPerspectivef(45.0f, 1.0f * width / height, 1.0f, 100.0f);
	
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
}


void pointer(UGWindow uwin, int button, int state, int x, int y)
{
	//MessageBox(NULL, "Xrot = " + *xrot + " Yrot = " + *yrot, "End",MB_OK);
	ugDestroyWindow(uwin);
}


void idle(UGWindow uwin)
{
	xrot += 2.0f;
	yrot += 3.0f;

	if(xrot > 360.0f)
	{
		xrot -= 360.0f;
	}
	if(yrot > 360.0f)
	{
		yrot -= 360.0f;
	}

	ugPostRedisplay(uwin);
}

int main()
{
	UGCtx ug = ugInit();
	UGWindow uwin = ugCreateWindow(ug, "UG_DEPTH", "Kips Window", 250, 250, 100, 100);
	if (!init())
	{
		return 1;
	}
	ugDisplayFunc(uwin, display);
	ugKeyboardFunc(uwin, keyboard);
	ugPointerFunc(uwin, pointer);
	ugReshapeFunc(uwin, reshape);
	ugIdleFunc(ug, idle);
	ugMainLoop(ug);

	return 0;


}

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.