Ive narrowed down the memory leak to this line of code and cant figure out whats causing it:
unscaledData = (unsigned char*)malloc((thisTexture->width*thisTexture->height)*4);
The codes used in loading a PCX texture file and the code leading upto that is this:
modelTexture_t *CUtility::LoadPCXTexture(char *filename)
{
PCXHEADER texInfo; // header of texture
modelTexture_t *thisTexture; // the texture
unsigned char *unscaledData = 0; // used to calculate pcx
int i; // index counter
int j; // index counter
int width; // width of texture
int height; // height of texture
// allocate memory for texture struct
thisTexture = (modelTexture_t*)malloc(sizeof(modelTexture_t));
if (thisTexture == NULL)
return NULL;
// load the PCX file into the texture struct
thisTexture->data = LoadPCXFile(filename, &texInfo);
if (thisTexture->data == NULL)
{
free(thisTexture->data);
return NULL;
}
// store the texture information
thisTexture->palette = texInfo.palette;
thisTexture->width = texInfo.xMax - texInfo.xMin ;
thisTexture->height = texInfo.yMax - texInfo.yMin ;
// allocate memory for the unscaled data
unscaledData = (unsigned char*)malloc((thisTexture->width*thisTexture->height)*4);
Im not sure if this is part of the problem but this is the code to load the actual file:
unsigned char* CUtility::LoadPCXFile(char *filename, PCXHEADER *pcxHeader)
{
int idx = 0; // counter index
int c; // used to retrieve a char from the file
int i; // counter index
int numRepeat;
FILE *filePtr; // file handle
int width; // pcx width
int height; // pcx height
unsigned char *pixelData; // pcx image data
unsigned char *paletteData; // pcx palette data
// open PCX file
filePtr = fopen(filename, "rb");
if (filePtr == NULL)
return NULL;
// retrieve first character; should be equal to 10
c = getc(filePtr);
if (c != 10)
{
fclose(filePtr);
return NULL;
}
// retrieve next character; should be equal to 5
c = getc(filePtr);
if (c != 5)
{
fclose(filePtr);
return NULL;
}
// reposition file pointer to beginning of file
rewind(filePtr);
// read 4 characters of data to skip
fgetc(filePtr);
fgetc(filePtr);
fgetc(filePtr);
fgetc(filePtr);
// retrieve leftmost x value of PCX
pcxHeader->xMin = fgetc(filePtr); // loword
pcxHeader->xMin |= fgetc(filePtr) << 8; // hiword
// retrieve bottom-most y value of PCX
pcxHeader->yMin = fgetc(filePtr); // loword
pcxHeader->yMin |= fgetc(filePtr) << 8; // hiword
// retrieve rightmost x value of PCX
pcxHeader->xMax = fgetc(filePtr); // loword
pcxHeader->xMax |= fgetc(filePtr) << 8; // hiword
// retrieve topmost y value of PCX
pcxHeader->yMax = fgetc(filePtr); // loword
pcxHeader->yMax |= fgetc(filePtr) << 8; // hiword
// calculate the width and height of the PCX
width = pcxHeader->xMax - pcxHeader->xMin + 1;
height = pcxHeader->yMax - pcxHeader->yMin + 1;
// allocate memory for PCX image data
pixelData = (unsigned char*)malloc(width*height);
// set file pointer to 128th byte of file, where the PCX image data starts
fseek(filePtr, 128, SEEK_SET);
// decode the pixel data and store
while (idx < (width*height))
{
c = getc(filePtr);
if (c > 0xbf)
{
numRepeat = 0x3f & c;
c = getc(filePtr);
for (i = 0; i < numRepeat; i++)
{
pixelData[idx++] = c;
}
}
else
pixelData[idx++] = c;
fflush(stdout);
}
// allocate memory for the PCX image palette
paletteData = (unsigned char*)malloc(768);
// palette is the last 769 bytes of the PCX file
fseek(filePtr, -769, SEEK_END);
// verify palette; first character should be 12
c = getc(filePtr);
if (c != 12)
{
fclose(filePtr);
return NULL;
}
// read and store all of palette
for (i = 0; i < 768; i++)
{
c = getc(filePtr);
paletteData = c;
}
// close file and store palette in header
fclose(filePtr);
pcxHeader->palette = paletteData;
// return the pixel image data
return pixelData;
}
Any help to get rid of this would be greatly appreciated.
[Edited by - Infinite_Daremo on July 1, 2008 5:23:00 AM]