Skip to main content
gamedev.net
🔒 Locked

Heap Corruption?

Started by CrazyCamel 5 replies 1.3k views
Original Post
CrazyCamel
CrazyCamel
This is a problem that I've been struggling with for a little bit now and I just can't figure out how to fix it. Basically, I'm creating a large array on the heap of type PIXEL, which is a struct containing 3 BYTE variables. The declaration of the array is like so:
PIXEL* pixel=new PIXEL[len];
Now the problem arises when I edit the information, such as calling memset((void*)pixel,0,len*sizeof(PIXEL)). Now, a call to _CrtCheckMemory() will return false, and if I try to delete [] pixel, the program crashes with the HEAP CORRUPTION DETECTED dialog box. Now, I think the reason for the problem is that I am trying to allocate too much memory, and that is causing the failure. When len is 7232, everything works fine, but when len is 331200, I get all the problems I just described (those specific numbers are the amount of bytes in two different files- the pixel array holds part of the file). I can't allocate less space, so I'm not sure what to do. I'm not even sure what specifically my problem is. Anyone know what's going on? Sorry if I didn't describe my problem well, its been a while since I last posted on a forum. Edit-- Known typos fixed, sorry about the confusion... [Edited by - CrazyCamel on July 14, 2006 12:27:42 AM]
Grab life by the cord.
Colin Jeanne
Colin Jeanne
You arent allocating 331200 bytes. You are allocating 331200 * sizeof(PIXEL) bytes. But even still, that's nothing in terms of space. This also means that your memset() call is incorrect. It should be memset((void*)pixel,0,len * sizeof(PIXEL)).

As for the problem, I would double check to ensure that you're staying within the bounds of your array. Heap corruption might mean that you're writing past the end of the array.
CrazyCamel
CrazyCamel
The memset/len thing was just a typo, heh.

As for writing past the bounds, I don't think I am, but I'm not sure. Here is the exact code:
pixel=new PIXEL[infoHeader.biSizeImage/sizeof(PIXEL)];memset((void*)pixel,0,infoHeader.biSizeImage);//much later in code:delete [] pixel;
That should work right? But starting from after I call memset(), _CrtCheckMemory() returns false.

Edit-- Also I should note that its not specifically the memset() function that is causing problems. If I take out memset and insert something like, say, fread() the problem persists:
pixel=new PIXEL[infoHeader.biSizeImage/sizeof(PIXEL)];fread((void*)pixel,infoHeader.biSizeImage,1,stream);//much later in code:delete [] pixel;
Grab life by the cord.
deathkrush
deathkrush
Quote:
Original post by CrazyCamel
The declaration of the array is like so:
PIXEL pixel=new PIXEL[len];
Now the problem arises when I edit the information, such as calling memset((void*)pixel,0,len). Now, a call to _CrtCheckMemory() will return false, and if I try to delete [] pixel, the program crashes with the HEAP CORRUPTION DETECTED dialog box.


I hope that's a typo, because the right way to allocate an array is:

PIXEL* pixel=new PIXEL[len];
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
CrazyCamel
CrazyCamel
Quote:
Original post by deathkrush

I hope that's a typo, because the right way to allocate an array is:

PIXEL* pixel=new PIXEL[len];


Heh, it's a typo, sorry.

Grab life by the cord.
deathkrush
deathkrush
Well, there is nothing wrong with

PIXEL* pixels = new PIXEL[len];
memset(pixels, 0, len * sizeof(PIXEL));
delete [] pixels;

You must be doing something wrong somewhere else, it would help if you posted the code.
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
CrazyCamel
CrazyCamel
Oh dear, sorry about all that, but it appears that the problem was related to a corrupt file (the code was built to read in a file)... anyway, I should probably delete this post, as I can't see it being any help to anyone...

Edit-- By the way, with all this editing, I've noticed a bug (or some really wierd feature) in gamedev.net programming... Every so often I see a bunch of wacky icon options in edit post mode, but when I select one of the wacky options and click Make Modifications, it simply shows the orange arrow... I liked the guy with the smoke... :(
Grab life by the cord.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.