C++ 如何释放GLU_-TESS_组合回调中分配的内存

C++ 如何释放GLU_-TESS_组合回调中分配的内存,c++,opengl,C++,Opengl,我用臀大肌来填充一些非凸多边形 它工作得很好,但是对于一些多边形,它抱怨它需要一个组合函数,所以我提供了一个非常简单的GLU_TESS_combine回调,它分配一个新的顶点,只复制坐标(它是带有纯色的2D,所以我不需要插值RGB值或任何东西): 现在一切都按预期呈现,但显然会泄漏内存。文件说: 分配另一个顶点,[…]之后某个时间释放内存 调用GrassSendPolygon 但在我找到的所有例子中,它们都没有显示如何处理内存。回调是自由函数,没有办法释放分配给它的内存,是吗 我能想到的唯一办法

我用臀大肌来填充一些非凸多边形

它工作得很好,但是对于一些多边形,它抱怨它需要一个组合函数,所以我提供了一个非常简单的GLU_TESS_combine回调,它分配一个新的顶点,只复制坐标(它是带有纯色的2D,所以我不需要插值RGB值或任何东西):

现在一切都按预期呈现,但显然会泄漏内存。文件说:

分配另一个顶点,[…]之后某个时间释放内存 调用GrassSendPolygon

但在我找到的所有例子中,它们都没有显示如何处理内存。回调是自由函数,没有办法释放分配给它的内存,是吗

我能想到的唯一办法是将它们存储在某个地方,然后自己删除它们。这是正确的方法吗?

看看

关键是不要在回调中分配任何内存(否则将导致内存泄漏)。相反,您应该将顶点数据复制到回调中的内存位置(就像在中所做的那样)。从何处复制顶点数据取决于您

以下是回调函数在其


好文章,好例子!我最终将新的顶点存储在一个向量中,但基本上是一样的。谢谢,这些文档不应该显示正确的例子,而不是那些泄漏内存的例子吗?@MikMik是的,它们应该,但在文档中我认为它们没有分配内存。它看起来是这样的:
VERTEX*new=new_VERTEX(),我确信他们不会在new_vertex()函数中分配新内存。@MikMik顺便说一句,MikMik始终使用opengl参考页,对于RullsCallback是
void CALLBACK tessCombine( GLdouble coords[3], GLdouble * vertex_data[4], GLfloat weight[4], GLdouble **outData )
{
    GLdouble *vertex = new GLdouble[3];
    vertex[0] = coords[0];
    vertex[1] = coords[1];
    vertex[2] = coords[2];
    *outData = vertex;
}
void CALLBACK tessCombineCB(const GLdouble newVertex[3], const GLdouble *neighborVertex[4],
                            const GLfloat neighborWeight[4], GLdouble **outData)
{
    // copy new intersect vertex to local array
    // Because newVertex is temporal and cannot be hold by tessellator until next
    // vertex callback called, it must be copied to the safe place in the app.
    // Once gluTessEndPolygon() called, then you can safly deallocate the array.
    vertices[vertexIndex][0] = newVertex[0];
    vertices[vertexIndex][1] = newVertex[1];
    vertices[vertexIndex][2] = newVertex[2];

    // compute vertex color with given weights and colors of 4 neighbors
    // the neighborVertex[4] must hold required info, in this case, color.
    // neighborVertex was actually the third param of gluTessVertex() and is
    // passed into here to compute the color of the intersect vertex.
    vertices[vertexIndex][3] = neighborWeight[0] * neighborVertex[0][3] +   // red
                               neighborWeight[1] * neighborVertex[1][3] +
                               neighborWeight[2] * neighborVertex[2][3] +
                               neighborWeight[3] * neighborVertex[3][3];
    vertices[vertexIndex][4] = neighborWeight[0] * neighborVertex[0][4] +   // green
                               neighborWeight[1] * neighborVertex[1][4] +
                               neighborWeight[2] * neighborVertex[2][4] +
                               neighborWeight[3] * neighborVertex[3][4];
    vertices[vertexIndex][5] = neighborWeight[0] * neighborVertex[0][5] +   // blue
                               neighborWeight[1] * neighborVertex[1][5] +
                               neighborWeight[2] * neighborVertex[2][5] +
                               neighborWeight[3] * neighborVertex[3][5];


    // return output data (vertex coords and others)
    *outData = vertices[vertexIndex];   // assign the address of new intersect vertex

    ++vertexIndex;  // increase index for next vertex
}