Memory allocation problem, C++ with new operator.
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!
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
Enigma
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!
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!
I think I may have spotted the problem:
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]
//read triangle dataunsigned 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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement