Sign in to follow this  

Freaking wierd memory allocation problem

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

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:
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;


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]

Share this post


Link to post
Share on other sites
Guest 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Just to be sure, make sure you have tried this:

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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