PDA

View Full Version : Win32 - Memory Allocation



Zeus_666
06-18-2001, 05:01 AM
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

Geniuz
06-18-2001, 05:11 AM
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

ffish
06-18-2001, 05:22 AM
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 http://www.opengl.org/discussion_boards/ubb/smile.gif

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 http://www.opengl.org/discussion_boards/ubb/smile.gif</edit>

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

Zeus_666
06-18-2001, 05:28 AM
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).]

ffish
06-18-2001, 05:36 AM
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).]

Zeus_666
06-18-2001, 05:48 AM
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

Tim Stirling
06-18-2001, 06:07 AM
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

Zeus_666
06-18-2001, 06:10 AM
Hi,
If you mean for my graphics card, i have the latest Nvidia Geforce 2 drivers but not the latest Detonator driver's.

ffish
06-18-2001, 06:22 AM
<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 http://www.opengl.org/discussion_boards/ubb/wink.gif

Zeus_666
06-18-2001, 06:33 AM
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 >> worldWidth;
fin >> worldDepth;
fin >> numberRectangles;
fin >> numberTriangles;

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

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

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

for(i = 0; i < (9 * numberRectangles); i++)
fin >> triColours[i];


fin.close(); //CLOSE THE FILE
}

-----------------------------------

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

if (worldWidth > worldDepth)
visabilityRange = worldWidth + 10;

if (worldDepth > 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.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2001, 06:43 AM
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. http://www.opengl.org/discussion_boards/ubb/smile.gif

Hope that helps.

Zeus_666
06-18-2001, 06:47 AM
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 ();
}

ffish
06-18-2001, 06:54 AM
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.

Michael Steinberg
06-18-2001, 10:46 AM
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.

Benjy
06-18-2001, 01:45 PM
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)

Zeus_666
06-18-2001, 09:45 PM
Hi,
Is there not a variable length array construct in c++?

ffish
06-18-2001, 10:52 PM
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.

Zeus_666
06-18-2001, 11:23 PM
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

ffish
06-19-2001, 12:27 AM
Dunno what you're drawing http://www.opengl.org/discussion_boards/ubb/smile.gif but yeah, it's working on my machine, too. Good luck with the level editor!

Zeus_666
06-19-2001, 12:33 AM
Originally posted by ffish:
Dunno what you're drawing http://www.opengl.org/discussion_boards/ubb/smile.gif but yeah, it's working on my machine, too. Good luck with the level editor!

Don't suppose you would like to help?

Michael Steinberg
06-19-2001, 04:57 AM
Allocating memory really dynamicaly is not exactly the best to do. Better is to allocate a huge block of memory, and inplement your own allocating inside it. You won't get memory fragmentation this way for example. This of course only applies to often used and big blocks of geometry, like model data or something like this. Also, your program can check for "memory violations" easily this way.

harsman
06-19-2001, 05:10 AM
Of course you'll get fragmentation, unless you only use it for one thing at a time. A good ground rule might be: If you have to start managing it yourself(writing a storage allocator etc.), let the OS do it for you.

Michael Steinberg
06-19-2001, 05:15 AM
I don't agree completely with your opinion, though it is correct for many things.
If you do your own allocating in a huge block, allocating is way faster! You could start allocating from the back and from the front. Then just move the pointers and return them. For debugging reasons, you could spare some bytes inbetween (or even just a single, but for crashing reasons a few more might be good).
Also, try to avoid reallocation, it's obvious why...

kistompika
06-19-2001, 05:15 AM
Static memory allocation is limited in Windows. You generally use Win32 API functions to allocate a huge amount of memory(GlobalAlloc(), LocalAlloc(), Virtualalloc, etc.), though dynamic memory allocation with the 'new' operator works pretty fine, but I don't know which method it uses.

Kistomika

ffish
06-19-2001, 05:28 PM
Don't suppose you would like to help?

I would but I'm in my final year of an engineering degree and I don't have the time. Sorry http://www.opengl.org/discussion_boards/ubb/smile.gif

harsman
06-19-2001, 11:19 PM
Yeah, it'll be faster to allocate big chunks, but then you get the extra cost of doing the management. My point was just that if you need to manage it yourself (in a non trivial way, it's not that hard to allocate AGP mem and fill it up with vertices when you need to render something) you better make sure sure your storage allocator is better than the one in the OS. I think you're more or less talking about memory pooling though, which is a good thing and easy to encapsulate.

Zeus_666
06-19-2001, 11:43 PM
Hi,
How does one tell how much of the graphics card memory they are using and does vertex data get stored there automatically?