简体   繁体   中英

Memory leak after deleting pointer

I have a memory leak and I have no idea why it happens. Please note, I removed most of the code that has nothing to do with the issue and most of it was given in the assignment, so I only wrote the Tetrahedron and the add_triangles(...) function:

class Geometry {
protected:
    unsigned int vao, vbo;
public:
    /* Generates 1 vertex buffer */
};

class Tetrahedron : public Geometry {
    std::vector<VertexData> vtxData;
    vec3 points[4];
public:
    Tetrahedron() : points { /* default points */ } { create(); }
    Tetrahedron(const vec3 p0, const vec3 p1, const vec3 p2, const vec3 p3) : points { p0, p1, p2, p3 } { create(); }
    void create() {
        /* Creates the 4 sides ans stores at vtxData */
        /* Passing the data to OpenGL */
    }
    void gen(std::vector<Tetrahedron*>& data, int stop_at = 0, float tend = 1, int depth = 0) {
        data.push_back(this);
        if (depth > stop_at) {
            return;
        }
        for (int i = 0; i < 4; i++) {
            /* calculate new points */
            (new Tetrahedron{ /* new points */ })->gen(data, stop_at, tend, depth + 1);
        }
    }
};

struct Object {
    Geometry* geometry;
public:
    /* Drawns and rotates the given geometry */
};

class Scene {
    std::vector<Object*> objects;
    std::vector<Tetrahedron*> thets;
public:
    void Build() {
        /* Setting up shaders, lights, materials, objects */
        add_triangles();
    }
    void add_triangles(float tend = 1) {
        for (unsigned int i = 0; i < objects.size(); i++) {
            delete objects[i];
        } std::vector<Object*>{}.swap(objects);

        for (unsigned int i = 0; i < thets.size(); i++) {
            delete thets[i];
        } std::vector<Tetrahedron*>{}.swap(thets);

        (new Tetrahedron{})->gen(thets, 1, tend);

        for (unsigned int i = 0; i < thets.size(); i++) {
            objects.push_back(new Object{ thets[i] });
        }
    }
    void Animate(float tstart, float tend) {
        /* ... */
        add_triangles(fabs(cosf(tend) * sinf(tend)) + 1);
        /* ... */
    }
    ~Scene() {
        for (unsigned int i = 0; i < objects.size(); i++) {
            delete objects[i];
        }
        for (unsigned int i = 0; i < thets.size(); i++) {
            delete thets[i];
        }
    }
};

For some reason the memory leak occurs when I don't store the Tetrahedrons in a separated vector and just deletes them in the Object's destructor:

~Object() { delete geometry; }

Why does it leak if it should be the same as deleting them in the for loop written in the add_triangles(...) function? - I used VS2019 to compile it.

To correct myself:

  • This code infact, does not leak, it deletes the Tetrahedrons individually.
  • If I want the Object to delete its Tetrahedron "geometry", it leaks.

Tetrahedron inherits from Geometry and you are deleting from a pointer to a Geometry object. The destructor of Tetrahedron is not called because you did not define the destructor of Geometry as virtual.

Add a virtual destructor to Geometry to allow the destructor of Tetrahedron to be called in this case.

class Geometry {
protected:
    // ...
public:
    virtual ~Geometry() {} // <= Virtual destructor
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM