cemcen / Delynoi

An object-oriented C++ library for the generation of polygonal meshes
18 stars 8 forks source link

Memory issue when returing static Mesh within getConformingDelaunayTriangulation() #9

Open ppizarror opened 1 year ago

ppizarror commented 1 year ago

Hi! I found another issue during triangulation.

If Delaunay is used consecutively, the mesh was already instantiated (because its singleton-pattern static). Thus, deleting its points and edges (calling our own .clear() method) generates a CxC000005 memory issue. This does not happen with Voronoi, as it does not return a static mesh.

Thus, instead of:

Mesh<Triangle> &TriangleDelaunayGenerator::getConformingDelaunayTriangulation() {
    if (!this->empty) {
        char switches[] = "pzejDQ";
        callTriangle(seedPoints, switches);
    }

    // Dangerous, even if the memory is not cleared, the mesh will not initialize again!
    static Mesh<Triangle> mesh = initializeMesh<Triangle>();
    return mesh;
}

The method returns:

Mesh<Triangle> &TriangleDelaunayGenerator::getConformingDelaunayTriangulation() {
    if (!this->empty) {
        char switches[] = "pzejDQ";
        callTriangle(seedPoints, switches);
    }
    if (this->meshInitialized) return this->mesh;
    this->mesh = initializeMesh<Triangle>();
    this->meshInitialized = true;
    return this->mesh;
}

Where this->mesh and this->meshInitialized class parameters, the problem is solved and works fantastic.

See https://github.com/IE3-CL/Delynoi for the updated version of this library.

ppizarror commented 1 year ago

Also, I tested thoroughly using Valgrind and no warnings whatsoever!