FinalImageData->Depth = 24;
FinalImageData->Height = Height;
FinalImageData->Width = Width;
FinalImageData->Data = new unsigned char[FinalImageData->Width * FinalImageData->Height * 3];
if(FinalImageData->Data == NULL)
return MEMORY_ALLOCATION_ERROR;
Freaking wierd memory allocation problem
I have struct that is defined as:
struct GenericImage
{
unsigned int Depth;
unsigned int Height;
unsigned int Depth;
unsigned char *Data;
};
I pass one of these into this function:
IMAGE_RETURN_CODE PCXImage::LoadImage(string FileName, GenericImage *FinalImageData)
where the error is taking place. The code where it is happening? Here:
In the debugger, I checked the width and height, and they are both equal to 64 (my image is 64x64). That is correct. So it tries to allocate 64 * 64 * 3 bytes (which is 12288 bytes, or 12 KB). It fails. It gives me null. My texture manager does the same thing with BMP and TGA files, and it works perfectly (hell, they're even the same size). When it tries to allocate memory for a PCX file, it fails. WTF?
[Edited by - xg0blin on August 28, 2004 1:24:23 PM]
You mean 12K, right? new shouldn't have a problem allocating that unless you are creating thousands of these.
Are you sure new is really returning null, and that you are checking "... == NULL" and not "... = NULL" somewhere.
Maybe post more code.
Are you sure new is really returning null, and that you are checking "... == NULL" and not "... = NULL" somewhere.
Maybe post more code.
Quote:Original post by Anonymous Poster
You mean 12K, right? new shouldn't have a problem allocating that unless you are creating thousands of these.
Are you sure new is really returning null, and that you are checking "... == NULL" and not "... = NULL" somewhere.
Maybe post more code.
Yeah, k (heh). I had a brainfart.
new is returning null. I go through that code with the debugger. It's like 0x0CCCCCCC before it goes to new, and it comes out 0x00000000. I don't know why. I'm only creating one.
Perhaps some code is trashing the heap. This is usually by writing past the end of an array.
Just to be sure, make sure you have tried this:
By breaking it up you may find that things you'd think are correct aren't. e.g. if height and width were shorts and they multiplied to over 32767 you'd see the error in the debugger this way.
typedef unsigned char uint8;int numBytes = FinalImageData->Width * FinalImageData->Height * 3;FinalImageData->Data = new uint8[numBytes];
By breaking it up you may find that things you'd think are correct aren't. e.g. if height and width were shorts and they multiplied to over 32767 you'd see the error in the debugger this way.
I don't know why, but I always do this when I can't think of anything else. Anyways, I did rebuild all, and well, it works. Freaking weird. I didn't change any code.
The problem is very likely that you're trashing the heap somewhere before that call to new, and the memory manager returns NULL because it detects the corruption.
It could be your GenericImage pointer that's bad, but it could also be something happening way before this function.
It could be your GenericImage pointer that's bad, but it could also be something happening way before this function.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement