Sign in to follow this  
disanti

Memory allocation problem, C++ with new operator.

Recommended Posts

Hello, I've been programming a model loader for my small game and I have run into a problem with the new operator. The below line of code crashes when built with MinGW, and returns NULL when built with MSVC++. It even fails on other computers, I checked the available memory on my system and I have 700mb free, I tried rebooting and it still fails. unsigned short* t = new unsigned short[current[currentTriangle]+1]; In the debugger, currentTriangle is 0, and current[0] is 341. Therefore it should be allocating 342 unsigned shorts worth the memory. The line of code is in a for loop with currentTriangle as the index from 0 to 1, although I doubt that that is the problem. Any help is appreciated!

Share this post


Link to post
Share on other sites
You've likely trashed your heap somewhere by writing out of bounds of an array. Check all your array accesses and, if you haven't already, run your program through a debugger and hope it'll catch your error.

Enigma

Share this post


Link to post
Share on other sites
OK, I did some debugging, this time looking at arrays and stuff (thanks Enigma). I figured that my array class was probably corrupting the heap in my filesystem class so I replaced it with the STL vector and now I get a new problem on that line of code:

Unhandled exception at 0x77e73887 in pss0.exe: Microsoft C++ exception: std::bad_alloc @ 0x0012f9ec.

That leads me to believe that my array class must have been doing something bad. Any suggestions on how I can find heap corruption easier?

Thanks!

Share this post


Link to post
Share on other sites
I think I may have spotted the problem:

//read triangle data
unsigned short a,b,c; char m;
short current[MAX_TEXTURES] = {-1};
for(currentTriangle = 0; currentTriangle < triangleCount; currentTriangle++)
{
fileSystem->readToShort(file,(short*)&a);
fileSystem->readToShort(file,(short*)&b);
fileSystem->readToShort(file,(short*)&c);
fileSystem->read(file,1,&m);
mesh->indices[m][current[m]++] = a;
mesh->indices[m][current[m]++] = b;
mesh->indices[m][current[m]++] = c;
}





I believe the problem resides with the [current[m]++]. According to what I've seen with the debugger, a buffer 'underrun' is probably occuring. It is accessing memory to the -1 index. I'm going to try setting it to 0 and see if that has any effect on the heap.

Edit: it is no longer 'underrunning' however I'm still having a crash on the same line of that allocates memory for the unsigned short t.

Edit 2: OK, I just rewrote a big chunk of the code, and now it seems to work alright. Thank you!

[Edited by - disanti on November 26, 2005 4:04:44 PM]

Share this post


Link to post
Share on other sites

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