我不确定以下代码是否可以避免内存泄漏。
#ifndef RENDERABLE_H
#define RENDERABLE_H
class QGLShaderProgram;
class GLWidget;
class BoundingBox;
class Renderable{
public:
virtual void update(float duration) = 0;
virtual void render(QGLShaderProgram& shader, float duration) = 0;
virtual BoundingBox* getBBox() const = 0;
virtual void translate(float xx, float yy, float zz) = 0;
virtual void rotate(float degrees_x, float degrees_y, float degrees_z) = 0;
virtual void scale(float xx, float yy, float zz) = 0;
};
#endif // RENDERABLE_H
上面的“接口”是由object3d.cpp实现的。然后,如果它们属于同一个场景,我们可以将许多 Object3D 对象添加到一个场景对象中。但是,在场景结束时,我想确保没有内存泄漏,所以我会在所有内容上调用 delete。但是,在场景对象中,我有以下变量:
QVector<Renderable*>* sceneObjects;
QVector<GLTexture2D*>* sceneTextures;
QMap<QString, Material*>* sceneMaterials;
如你看到的,
delete sceneObjects;
delete sceneTextures;
delete sceneMaterials;
应该删除 QVector 并且根据 Qt,它应该调用其中那些对象的析构函数。然而,Qt 文档并不清楚对象指针。Qt 会使用适当的析构函数删除对象指针吗?此外,Renderable 指针会发生什么?从“接口”可以看出它没有析构函数。
感谢您的任何意见。ChaoSX恶魔