Jump to content
  • Advertisement
Sign in to follow this  
Mantear

HEAP: Heap Free block

This topic is 4997 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'm attempting to make the memory for my data in my program dynamic. I have a data structure I'll call sObject. sObject is declared as sObject* Object so I can dynamically allocate Object as the number of objects I have grows and shrinks. Within sObject, I have a 2 dimentional array of floats, float** Data, which I also want to be dynamic. When I go to allocate new memory when I want to grow the size of Object, I do this:
sObject *temp;
temp = new sObject[NewSize];
On the new operation, I get a pop-up window with the following error:
User breakpoint called from code at 0x7c901230
In the debug window, it says:
HEAP[programname.exe]: HEAP:  Free Heap block 21f93b08 modified at 21f93b18 after it was freed
The last piece of code executed before this all happens is:
return HeapAlloc(_crtheap, 0, size);
from VC98\CRT\SRC\MALLOC.C Anyone know what I'm doing wrong and what I can do to fix this problem?

Share this post


Link to post
Share on other sites
Advertisement
Sounds like heap corruption. This probably occurred in an earlier point of the program. Your object allocation is probably fine. Go through earlier points in the program and check pointers and heap reading/writing. There is a really useful CRT debugging function that checks if the heap is corrupted (I forget exactly what it is, shouldn't be hard to find), try calling that before you allocate this memory and I bet it says that it's corrupted.

Share this post


Link to post
Share on other sites
You were correct, I was corrupting the heap stack earlier. This leads into asking how to properly allocate memory dynamically. Using the same example.

I've got a structure sObject that contains a 2 dimentional array of floats, a couple unsigned ints, and another structure I'll call Struct1. Struct1 contains another 2 dimentional array of floats and two more unsigned ints. I want the 2 dimentional array of floats in both Struct1 and sObject to be dynamically allocatable, as well as sObject itself. (sObject is declared in the main data structure of my program). So it looks something like:

typedef struct
{
float Data[SOME_NUMBER][SOME_NUMBER];
unsigned int X;
unsigned int Y;
} Struct1;

typedef struct
{
float Data[SOME_NUMBER][SOME_NUMBER];
unsigned int X;
unsigned int Y;

Struct1 MoreData;
} sObject;

typedef struct
{
// Main data structure
...

sObject Object[SOME_NUMBER];
} MainData;

I want to make Object dynamic, Data to be dynamic, and MoreData to be dynamic. How do I do this?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!