Win32 - Memory Allocation

Hi,
This question is mainly about windows, i have a

#define MAX_SIZE 125000

that i use for array sizes to store data read from a text file. Now when i define array, that use more than 250000, my program crashes just after opening the OGL window.

GLfloat rectangles[MAX_SIZE];
GLfloat triangles[MAX_SIZE];
GLfloat quadColours[MAX_SIZE];
GLfloat triColours[MAX_SIZE];

Above are my declarations, any idea what is happening or any advice would be most grateful.

Thanks, Paul

Hi there,
Why don’t you allocate memory for your arrays dynamically?

Like:
GLfloat *rectangles=new GLfloat[num_of_rectangles];

My wild guess (Just thinking about old dos problems):
Your “solution” uses MAX_SIZE * 4 * sizeof(GLfloat) bytes for the arrays on the heap (somebody, correct me if i’m wrong). “My” “solution” uses 4 * sizeof(GLfloat) bytes for the arrays on the heap and the dynamic memory somewhere else (dunno g). I guess the heap is limited somehow in OS, while the other memory is limited by your RAM…

Please correct me if i am wrong, thx

Does the crash happen when you’re trying to read from the files? Or before? One of the most valuable tips I can pass on if you don’t know where is the line: std::cout << "Here in file: " << FILE << ", line: " << LINE << std::endl;
Good old stdout debugging

Unless you have a very old computer, you won’t be running out of RAM. I would guess it’s an access violation. What error do you get? Also, have you thought about using the C++ STL vector container. I use them probably more than arrays and they’re very handy - no more buffer overflows. They may be a fraction slower (or maybe a lot) but their usefulness makes up for it.

<edit>BTW Geniuz, you’ve got your memories mixed up. Heap memory is dynamically allocated, stack memory managed by the system </edit>

[This message has been edited by ffish (edited 06-18-2001).]

Hi,
When the program crashes, i am left with the following,

Loaded ‘C:\WINDOWS\SYSTEM\WINMM.DLL’, no matching symbolic information found.
Loaded ‘C:\WINDOWS\SYSTEM\glut32.dll’, no matching symbolic information found.
Loaded ‘C:\WINDOWS\SYSTEM\DCIMAN32.DLL’, no matching symbolic information found.
Loaded ‘C:\WINDOWS\SYSTEM\USER32.DLL’, no matching symbolic information found.
Loaded ‘C:\WINDOWS\SYSTEM\GLU32.DLL’, no matching symbolic information found.
Loaded ‘C:\WINDOWS\SYSTEM\GDI32.DLL’, no matching symbolic information found.
Loaded ‘C:\WINDOWS\SYSTEM\ADVAPI32.DLL’, no matching symbolic information found.
Loaded ‘C:\WINDOWS\SYSTEM\KERNEL32.DLL’, no matching symbolic information found.
Loaded ‘C:\WINDOWS\SYSTEM\CRTDLL.DLL’, no matching symbolic information found.
Loaded ‘C:\WINDOWS\SYSTEM\OPENGL32.DLL’, no matching symbolic information found.
Loaded ‘C:\WINDOWS\SYSTEM\VERSION.DLL’, no matching symbolic information found.
Loaded ‘C:\WINDOWS\SYSTEM\NTDLL.DLL’, no matching symbolic information found.
Loaded ‘C:\WINDOWS\SYSTEM\DDRAW.DLL’, no matching symbolic information found.
Loaded ‘C:\WINDOWS\SYSTEM\NVARCH32.DLL’, no matching symbolic information found.
Loaded ‘C:\WINDOWS\SYSTEM\NVOPENGL.DLL’, no matching symbolic information found.
First-chance exception in My Game V01.exe (NVOPENGL.DLL): 0xC0000005: Access Violation.
First-chance exception in My Game V01.exe (NVOPENGL.DLL): 0xC0000005: Access Violation.
First-chance exception in My Game V01.exe (NVOPENGL.DLL): 0xC0000005: Access Violation.
The program ‘C:\Windows\Desktop\OpenGL\Projects\My Game V01\Debug\My Game V01.exe’ has exited with code 0 (0x0).

[This message has been edited by Zeus_666 (edited 06-18-2001).]

Hmmm, <scratches chin/> first thoughts were an access violation, but not in NVOpenGL.dll. I don’t know without debugging it. You can send it to me if you like (e-mail in profile) but I won’t look at it until tomorrow (exam in the morning) or you can debug it yourself (much more valuable to you).

If you want to do it yourself, stick descriptive printf/cout statements (like my post above) in places where you think the problem might be. Find the exact line where the error occurs in your source and try to determine the source of the error. Make sure you know what the program state should be before and after the error. If the problem is an OpenGL one, become familiar with glGetError(). Also, become familiar with a good debugger. VC++'s one is quite good.

Debugging and rigorous testing suck, but they are the best way to learn how to program better. <rant>Buy a book on testing and learn how to do unit testing, structural testing, integration testing, etc</rant>.

Cheers.

<edit>Oh, now that you’ve edited it I see it’s a different problem. I would think it’s a project settings problem, maybe. Try starting a new project then add your source files to it. Again, you’re welcome to send the source to me (zipfile or .tar.gz, e-mail in profile, remove the nospam part from the address)</edit>

[This message has been edited by ffish (edited 06-18-2001).]

Hi,
Ok, step by step debugging as i hate it. After commenting out line after line (i will make fewer changes in one go next time) i have discovered that the error is caused by the addition of the line

glEnableClientState(GL_COLOR_ARRAY);

When my program tries to draw and this line is uncommented, the program will crash.


O, while you are still online, i would like to thank you for your help today as you appear to have been online all day and Good luck in the exam tomorrow.

Thanks, Paul

Does any one no what the lines like this

Loaded ‘C:\WINDOWS\SYSTEM\DDRAW.DLL’, no matching symbolic information found.

are all about? I have them everywhere in my progams, don’t think it is a problem.

As for the problem in hand (well on the monitor) I don’t really know, it sounds like an Opnegl problem but they don’t really exist and are certainly very rare. Have you got the latest drivers?

Tim

Hi,
If you mean for my graphics card, i have the latest Nvidia Geforce 2 drivers but not the latest Detonator driver’s.

<Changes mind again/>That information comes from the bottom line in your debugger, right? I get that "no matching … " crap too. It’s the line with the access violation that is important.

Although glEnableClientState shouldn’t produce errors like this often, I do remember having problems with it in the past. Can you post, step by step, how you set up your vertex arrays, and how you render them? I would think that your vertex array is trying to render from an array where you are trying to go outside the array’s bounds. I’d almost put money on it, in fact. I want to know how you fill up your array with data, how you let OpenGL know where the data is (gl{Vertex/Color/etc}Pointer), the rendering function (glDraw{Elements/Arrays}) and the location of all of these relative to each other.

It can’t hurt to download the latest drivers (Det 12.60?) if you have the time also.

Tim, that line is the secret line that tells us OpenGL is just a clever wrapper for Direct Draw

Hi,
Below is all the code applicable

int numberRectangles;
int numberTriangles;

GLfloat rectangles[MAX_SIZE];
GLfloat triangles[MAX_SIZE];
GLfloat quadColours[MAX_SIZE];
GLfloat triColours[MAX_SIZE];


void init (void)
{
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnableClientState(GL_VERTEX_ARRAY);
//glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
}


void OpenMapAndParse ()
{
ifstream fin; //FILE POINTER TO MAP FILE
// float rectangles[MAX_SIZE];
int i;

fin.open("map1.map");	//OPEN THE FILE

//GET WORLD DATA
fin &gt;&gt; worldWidth;
fin &gt;&gt; worldDepth;
fin &gt;&gt; numberRectangles;
fin &gt;&gt; numberTriangles;

//READ RECTANGLE VERTEX DATA
for(i = 0; i &lt; (12 * numberRectangles); i++)
	fin &gt;&gt; rectangles[i];

//READ TRIANGLE VERTEX DATA
for(i = 0; i &lt; (9 * numberTriangles); i++)
	fin &gt;&gt; triangles[i];

//READ QUAD COLOURS DATA
for(i = 0; i &lt; (12 * numberRectangles); i++)
	fin &gt;&gt; quadColours[i];

for(i = 0; i &lt; (9 * numberRectangles); i++)
	fin &gt;&gt; triColours[i];


fin.close();			//CLOSE THE FILE

}


int main(int argc, char** argv)
{
//C++ STUFF 1ST
OpenMapAndParse ();

if (worldWidth &gt; worldDepth)
	visabilityRange = worldWidth + 10;

if (worldDepth &gt; worldWidth)
	visabilityRange = worldDepth + 10;

//OPENGL STUFF 2ND
glutInit				( &argc, argv            );
glutInitDisplayMode		( GLUT_DOUBLE | GLUT_RGB );
glutInitWindowSize		( 700, 700               ); 
glutInitWindowPosition	( 100, 100               );
glutCreateWindow		( "hello"                );
init					();
glutDisplayFunc			( display                ); 
glutReshapeFunc			( reshape                );
glutKeyboardFunc		( keyboard               );
glutSpecialFunc			( keyboardSpecial        );
glutMainLoop			();
return 0;   

}


That is a large part of my code, It appears to be working now though, I think it may have something to do with the leaving out of the glColorPointer, i had not placed that line in at the time. The map data is as follows now:

60
40
7
4

-25.0 6.0 -5.0 -25.0 6.0 -35.0 -25.0 -1.0 -35.0 -25.0 -1.0 -5.0
25.0 6.0 -35.0 -25.0 6.0 -35.0 -25.0 -1.0 -35.0 25.0 -1.0 -35.0
25.0 6.0 -5.0 25.0 6.0 -35.0 25.0 -1.0 -35.0 25.0 -1.0 -5.0
25.0 6.0 -5.0 2.0 6.0 -5.0 2.0 -1.0 -5.0 25.0 -1.0 -5.0
2.0 6.0 -5.0 -5.0 6.0 -5.0 -5.0 3.0 -5.0 2.0 3.0 -5.0
-2.0 6.0 -5.0 -25.0 6.0 -5.0 -25.0 -1.0 -5.0 -2.0 -1.0 -5.0

12.0 2.0 -5.0 12.0 2.0 -14.0 12.0 -1.0 -14.0 12.0 -1.0 -5.0

-25.0 6.0 -35.0 -25.0 6.0 -5.0 0.0 12.0 -20.0
-25.0 6.0 -35.0 25.0 6.0 -35.0 0.0 12.0 -20.0
25.0 6.0 -35.0 25.0 6.0 -5.0 0.0 12.0 -20.0
-25.0 6.0 -5.0 25.0 6.0 -5.0 0.0 12.0 -20.0

1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0
1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0
1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0
1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0
1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0
1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0

0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

1.0 1.0 0.0 1.0 1.0 0.0 1.0 1.0 1.0
1.0 1.0 0.0 1.0 1.0 0.0 1.0 1.0 1.0
1.0 1.0 0.0 1.0 1.0 0.0 1.0 1.0 1.0
1.0 1.0 0.0 1.0 1.0 0.0 1.0 1.0 1.0

Thanks, Paul

Doh! You left out the most important part. It’s the gl{Vertex/Color/Index/Normal/TexCoord/EdgeFlag}Pointer line that’s the most important. It tells OpenGL where the data is. That’s where your error was. In order, you have to do the following with vertex arrays:
(i) Enable client states.
(ii) Put data into the various arrays (with the line above).
(iii) Dereference the arrays to render the data.
(iv) (Optional, depending on circumstance) Disable client states.
(v) Wash, rinse, repeat.

Hope that helps.

Here’s the rest, sorry missed it

void display(void)
{
int i = 0;
//CLEAR SCREEN TO BLACK
glClearColor (0.0, 0.0, 1.0, 0.0);
glClearDepth (1.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//SETUP THE CAMERA
glLoadIdentity ( );							
gluLookAt(x, y, z, 
	      x + lx,y + ly,z + lz,
		  0.0f,1.0f,0.0f);

//DRAW WORLD BOUNDARIES
DrawInitialWorld (worldWidth, worldDepth - (2 * worldDepth));

glColor3f (1.0, 0.0, 0.0);
//PERFROM DRAWING

//Draw Quads
glColorPointer (3, GL_FLOAT, 0, quadColours);
glVertexPointer (3, GL_FLOAT, 0, rectangles);
glDrawArrays (GL_QUADS, 0, (numberRectangles * 4));

glColor3f (1.0, 1.0, 1.0);

//Draw Triangles
glColorPointer (3, GL_FLOAT, 0, triColours);
glVertexPointer (3, GL_FLOAT, 0, triangles);
glDrawArrays (GL_TRIANGLES, 0, (numberTriangles * 3));

glutSwapBuffers ();

}

Looks alright to me. You may have to disable/reenable the client states between the quad and triangle rendering, though, I don’t remember. It’s late now in Oz (midnight) <yawn/> so I’ll run it tomorrow, unless someone else finds the problem first. Looks alright at first glance, though.

Seeya.

I rather think the “symbolic” thing comes from the debugger. It tries to get the source code for the dll’s somewhere (what it can if they’ve been compiled with debug mode). You’ll most probably get the same message for your .exe file if you compiled with “release” configuration.

That’s a nasty way to allocate memory - just allocating a huge array like that is very wasteful and also imposes uneccessary limits on your program. As someone else already said, just work out the total number of each data type and then dynamically allocate enough memory:

GLfloat* rectangles=new GLfloat[size];

that way, you use less memory(probably) and your code is much less likely to have trouble in future (ie array overruns)

Hi,
Is there not a variable length array construct in c++?

STL vectors are variable length containers. SGI has a sample STL implementation and documentation somewhere on their site. Is your code working now? I’m just about to have a look at it.

Hi,
Finally got the code working and am quite impressed with what i have coded, i am taking the advice of some of the posts and will be altering the memory allocation declarations so as not to waste memory. Although it is not much of a problem with my systems RAM 256Mb but if i wan t to port it, i will need to recode the memory allocation and i may as well do that now.

Paul

Dunno what you’re drawing but yeah, it’s working on my machine, too. Good luck with the level editor!

Originally posted by ffish:
Dunno what you’re drawing but yeah, it’s working on my machine, too. Good luck with the level editor!

Don’t suppose you would like to help?