Heap Corruption occurring in gluTessVertex - Please help

I am trying to tesselate the interior of an irregular polygon with the following C++/Psuedo code:

I declare the following function:

vertexCallback(GLdouble *vertex)
const GLdouble *pointer;

pointer = (GLdouble *) vertex;

All the others callbacks (combine, begin, end, error) are declared as well.

I also declare the following struct:

typedef struct
double x, y, z;
} coordinate;

I created a vector as follows:

vector <coordinate> TessPath;

To add a vertex, I do the following:

coordinate loc;
loc.x = x;
loc.y = y;
loc.z = 0.0;

gluTessVertex(tess,reinterpret_cast<double*>(&path[path.size() - 1]),reinterpret_cast<void *>(&path[path.size() - 1]));

The Tessellations are really messed up, going all over the screen.

When I run the program under valgrind, it gives the following:
==12310== Invalid read of size 4
==12310== at 0x410CD29: (within /usr/lib/libGL.so.1.2)
==12310== by 0x40F3C79: glVertex3dv (in /usr/lib/libGL.so.1.2)
==12310== by 0x408103E: (within /usr/lib/libGLU.so.1.3.060402)
==12310== by 0x40809EE: __gl_renderMesh (in /usr/lib/libGLU.so.1.3.060402)
==12310== by 0x40844C3: gluTessEndPolygon (in /usr/lib/libGLU.so.1.3.060402)
==12310== by 0x8067E28: Symbol::render() (in /home/fordj/Desktop/mlp/a.out)

What in the world am I doing wrong??? I have corrupted the heap somehow. When I comment out all of the Tessellation code, it runs beautifully sans filled interiors and valgrind has no complaints. The gluTessVertex, which in turn is calling glVertex3dv is the problem I believe.

Don’t use a vector in a loop, as its memory is dynamically allocated/freed between consecutive push_back() calls. gluTessVertex needs that the memory locations which are constant and do not change between consecutive calls.

Instead of the following

while (...)
  gluTessVertex(tess, ...)

do something like this:


for(uint i = 0; i < path.size(); ++i)
{  gluTessVertex(tess,reinterpret_cast<double*>(&path[i]),reinterpret_cast<void *>(&path[i]));

Nice idea, I think I’m going to go with it. I will never be bitten by behind the scenes allocation again!!