Skip to main content
gamedev.net
🔒 Locked

Please help

Started by ATronic Nov 29, 2001 at 7:42 PM 8 replies 250+ views
Original Post
ATronic
ATronic
Hey, I've got this model format of mine. One thing it's loader does looks like this.
      
GLfloat (*mesh)[3];
mesh = new GLfloat[polycount*3][3];
  
This part crashes every time a load a large model. The smallest one that has crashed this line is a 2881 polycount model. That would be allocating it like this.
        
mesh = new GLfloat[8643][3];
  
Is there some known reason as to why this would happen? Small models work just fine. I do not know exactly at what polycount it starts to crash, but I do know it's big models that do it. Thanks for any help. Alex Broadwin A-Tronic Software & Design ----- "if you fail in life, you were destined to fail. If you suceed in life, call me." "The answer is out there." "Please help, I'm using Windows!" Edited by - ATronic on November 29, 2001 8:43:10 PM
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"
NightShade
NightShade
The code you included seems to work fine for me.

My guess is it''s something else going out of bounds somewhere.

Feel free to post some more code.
-ns-
ATronic
ATronic
I''ll look again, but I debugged it. It crashed in the kernel while moving the memory block under the "new" function. I''ll go poke my code some more and see what I can come up with.

Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"
ATronic
ATronic
I tried a few things. While I did find a few un-initialized variables, I did not find the problem. Here's the function.

    
bool LoadModel(char* filename)
{
FILE *modelFile = NULL;
int vertex=0;
//Open The File To Read Binary

modelFile=fopen(filename, "rb");
//Make Sure File Was Opened

if(!modelFile)
return false;
//Define Model Header

int modelHeader[4];
int modelPolycount=0;
//Read File Header (textured?, colored?, alpha?, normals?, polycount?)

fread(&modelHeader[0], sizeof(int), 1, modelFile);
fread(&modelHeader[1], sizeof(int), 1, modelFile);
fread(&modelHeader[2], sizeof(int), 1, modelFile);
fread(&modelHeader[3], sizeof(int), 1, modelFile);
fread(&modelPolycount, sizeof(int), 1, modelFile);
//Record Header Data For Later Use

polycount=modelPolycount;
if(modelHeader[0]==1)
textured=true;
if(modelHeader[1]==1)
colored=true;
if(modelHeader[2]==1)
hasAlpha=true;
if(modelHeader[3]==1)
hasNormals=true;
//Create Actual Variables

GLfloat (*modelVertexData)[3] = NULL;
GLfloat (*modelTextureCoordinates)[2] = NULL;
GLfloat (*modelColorData)[4] = NULL;
GLfloat (*modelNormalData)[3] = NULL;

modelVertexData = new GLfloat[modelPolycount*3][3];
modelTextureCoordinates = new GLfloat[modelPolycount*3][2];
modelColorData = new GLfloat[modelPolycount*3][4];
modelNormalData = new GLfloat[modelPolycount*3][3];
//Load Information From File

for(vertex=0; vertex<(modelPolycount*3); vertex++)
{
//Load Vertex Data

fread(&modelVertexData[vertex][0], sizeof(GLfloat), 1, modelFile);
fread(&modelVertexData[vertex][1], sizeof(GLfloat), 1, modelFile);
fread(&modelVertexData[vertex][2], sizeof(GLfloat), 1, modelFile);
//Load Texture Data (if it exists)

if(modelHeader[0]==1)
{
fread(&modelTextureCoordinates[vertex][0], sizeof(GLfloat), 1, modelFile);
fread(&modelTextureCoordinates[vertex][1], sizeof(GLfloat), 1, modelFile);
}
//Load Color Data (if it exists)

if(modelHeader[1]==1)
{
fread(&modelColorData[vertex][0], sizeof(GLfloat), 1, modelFile);
fread(&modelColorData[vertex][1], sizeof(GLfloat), 1, modelFile);
fread(&modelColorData[vertex][2], sizeof(GLfloat), 1, modelFile);
//If Header Specified Alpha, Get Alpha Data From File

if(modelHeader[2]==1)
fread(&modelColorData[vertex][3], sizeof(GLfloat), 1, modelFile);
else
modelColorData[vertex][4]=1.0f;
}
//Load Normal Data (if it exists)

if(modelHeader[3]==1)
{
fread(&modelNormalData[vertex][0], sizeof(GLfloat), 1, modelFile);
fread(&modelNormalData[vertex][1], sizeof(GLfloat), 1, modelFile);
fread(&modelNormalData[vertex][2], sizeof(GLfloat), 1, modelFile);
}
}
//Close The File

fclose(modelFile);
//Put The Model Data Into The Vertex Array

model->AddVertexData(3*modelPolycount, modelVertexData);
if(modelHeader[1]==1)
model->AddColorData(3*modelPolycount, modelColorData);
if(modelHeader[0]==1)
model->AddTexCoordData(3*modelPolycount, modelTextureCoordinates);
if(modelHeader[3]==1)
model->AddNormalData(3*modelPolycount, modelNormalData);
//Free Up Allocated Memory

delete[] modelVertexData;
delete[] modelTextureCoordinates;
delete[] modelColorData;
delete[] modelNormalData;
//Say Everything Went OK

loaded=true;
return true;
}


Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I'm using Windows!"

Edited by - ATronic on November 29, 2001 11:32:56 PM
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"
ATronic
ATronic
It fails on the first "new" statement. In every debug session, it has been all posative values. Plus I write those values to the file myself. Unsigned you say? Hmmm... sounds like a memory saver to me! Anyone else? I could use ideas here.
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"
Sturm
Sturm
Okay I''ve just made some simple test using the following code

  
int (*i)[3];

i = new int[0xFFFFFF * 3][3];

(*i)[0] = 6;

delete i;


This didn''t die on me (Just allocated RAM). Now this brought my computer down on its knees (Total allocation approx 140 MB). The only time this failed was when doing this:

  
i = new int[0xFFFFFFFF * 3][3];


But that makes sence since I cannot allocate that mem. But new worked it just returned NULL. So I got the failure on (*i)[0] = 6; since i pointed to NULL.

The only thing that I can come up with is that either you or GLfloat overloaded the new.

One more thing though what OS are you working on? I might be that. I''m using WindowsXP and no problem here. Do you get the same using the above code?

-- Sturm

---------------------------------------------------
Life after death? No thanks, I want to live NOW
--- Sturm 2001
---------------------------------------------------Life after death? No thanks, I want to live NOW --- Sturm 2001
ATronic
ATronic
I am using windows 98 se. I should probably check if it returns null. I am at another computer at this time, and will go check that in a few.

Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"
ATronic
ATronic
It does not get that far. It crashes under link_new_block (malloc). Actually, it gets to "mov" in kernel32.dll! Oddish in my opinion. Anyone have an idea why, I see no other problems with the code, and it works with smaller models!! Argh!

Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"
NightShade
NightShade
Well I tried a slightly modified version of the code above, it seems to work fine, so here are my best tips:

Maybe this function is part of a class that''s either not allocated, or allocated improperly (if it''s not part of a class, make sure any global variables it uses are correctly allocated).

Maybe memory has been hosed earlier (sometimes if I try to assign array values using a negative index, it will hose memory, but will die later on). Also look for improper freeing/deleting and other memory problems, all earlier on.

I dont know much about opengl, but I assume a GLfloat is just a typedef of a float. Does it crash the same way if you just allocate a float[] array instead of a GLFloat?

If you have access to BoundsChecker, this can sometimes help track down these kinds of things.

Hope this helps.

-ns-

Topic Locked

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

Sign in to reply to this topic.