PDA

View Full Version : Memory problem



Hylke Donker
12-19-2005, 11:51 AM
Hello,
I know this question does not have much to do with OpenGL(it's more a C++ question), but I hope that y'all don't mind that i'm posting it here.
What my problem is: my variables do not life long enough(or they change position).
Because when I call glArrayElement(0); I get a segmentation fault, because of an invalid read.
This is basicly what my code comes down to:
header:

struct Polygon { float data[9]; };
[..]
vector<Polygon> PolygonData;cpp file:
Polygon VertexDat;
for(int i = 0;i < 3;i++)
{
for(int j = 0;j < 3;j++)
{
VertexDat.data[3*i+j] = mesh->pointL[f->points[i]].pos[j];

}
}
PolygonData.push_back(VertexDat);
glVertexPointer(3, GL_FLOAT, 0, (*PolygonData.end()).data);
glBegin(GL_TRIANGLES);
glArrayElement(0);
glArrayElement(1);
glArrayElement(2);
glEnd();Does anyone who/what is causing this problem? Because to my, the code looks pretty memory leak less.
Thanx in advance,
Hylke

Jan
12-19-2005, 02:27 PM
You store your data in an STL vector and then try to give OpenGL the adress of that vector for rendering??

Uh, that just cries out loud for trouble...

You do know, that the STL encapsulates a lot of hairy details? You do know, that pushing just one more polygon to your vector, the whole memory can be relocated?

Also i don't know what you are trying to do with (*PolygonData.end()).data. That looks very suspicious to me.

You need to give OpenGL a pointer to the BEGINNING of you data, not to the end.

Here's my advice:

1. Learn more about the STL and how it works.
2. Read the documentation of glVertexPointer (and vertex arrays in general)
3. Don't use the STL for such a case, use static arrays (which you allocate YOURSELF, manually via new/delete)

Hope that helps.
Jan.

songho
12-19-2005, 05:20 PM
The member function, end() returns a special kind of object pointer(iterator) which is pointing past the last entry in the container. So you cannot access to it. (no valid data on it)

You may get the last entry like this;
PolygonData.at( PolygonData.size()-1 ).data

And, it is better

PolygonData.begin()->datathan

(*PolygonData.begin()).dataI switched end() to begin() because end() won't work.

Regards.

Hylke Donker
12-22-2005, 10:26 AM
Originally posted by Jan:
You store your data in an STL vector and then try to give OpenGL the adress of that vector for rendering??

Uh, that just cries out loud for trouble...

You do know, that the STL encapsulates a lot of hairy details? You do know, that pushing just one more polygon to your vector, the whole memory can be relocated?

Also i don't know what you are trying to do with (*PolygonData.end()).data. That looks very suspicious to me.

You need to give OpenGL a pointer to the BEGINNING of you data, not to the end.

Here's my advice:

1. Learn more about the STL and how it works.
2. Read the documentation of glVertexPointer (and vertex arrays in general)
3. Don't use the STL for such a case, use static arrays (which you allocate YOURSELF, manually via new/delete)

Hope that helps.
Jan.To clear things up: I don't want to use my vector as the data array, but the variable data, from the element at the end of that vector.
What I just found out was that you can't do this:
*myvector.end()
I have to have an iterator to do so, so the following code does work:
vector<TYPE>::iterator it = myvector.end();
*it.end();
Which is equivalent to:
myvector.at( myvector.size()-1 );
Thanks y'all,
Hylke

Hylke Donker
12-24-2005, 12:43 AM
Somehow i'm still having problems, but i'm almost starting to think the problem is not the memory.
My code:

PolygonData.push_back(VertexDat);
vector<Polygon>::iterator PoDaIT = PolygonData.end();
float *ptr = (*PoDaIT).data;
glVertexPointer(3, GL_FLOAT, 0, ptr);
cout << ptr << ": " << *ptr << endl;
for(int k = 0;k < 8;k++)
{
ptr++;
cout << ptr << ": " << *ptr << endl;
}
[..]
glBegin(GL_TRIANGLES);
/*glVertex3f(VertexDat[0], VertexDat[1], VertexDat[2]);
glVertex3f(VertexDat[3], VertexDat[4], VertexDat[5]);
glVertex3f(VertexDat[6], VertexDat[7], VertexDat[8]);*/
glArrayElement(0);
glArrayElement(1);
glArrayElement(2);
glEnd();Output:

0x826fe24: 2.38221e-44
0x826fe28: 5.02186e-34
0x826fe2c: 5.77156
0x826fe30: 0
0x826fe34: 3.50325e-44
0x826fe38: 4.92398e-34
0x826fe3c: 4.9406e-34
0x826fe40: 5.02193e-34
0x826fe44: 5.03484e-34
Before 0

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 16384 (LWP 4995)]
0x41376894 in _nv000098gl () from /usr/lib/libGLcore.so.1Any idea what could be wrong?
Thanx,
Hylke

songho
12-24-2005, 01:23 PM
Hylke Donker,
Once again, There are is no valid data in vector::end(). It returns the beyond (one past) of the last entry. And, it is not equivalent to vector::at(size-1).

If you want to access the last element of container, you may also look at vector::back(). However, it returns a reference to the last element in the container, instead of iterator.

Regards.
==song==