ma Model类中的问题是处理由它组成的每个网格。当它由7个网格组成时,首先将析构函数调用21次。退出该计划后,它被召唤了14次。真的不知道我做错了什么。我能够将问题拖到这个特定的代码片段中。
void Model::processNode(aiNode *node, const aiScene *scene) {
for(unsigned int i = 0; i < node->mNumMeshes; i++) {
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
mMeshes.push_back(processMesh(mesh, scene));
}
for(unsigned int i = 0; i < node->mNumChildren; i++) {
processNode(node->mChildren[i], scene);
}
}
mMeshes是单个Model对象的网格的私有向量。
编辑:
澄清一下,问题是当Mesh类的析构函数没有被注释掉时,它会完全破坏mMeshes向量中的所有内容。这是Mesh
的析构函数Mesh::~Mesh () {
glDeleteVertexArrays(1, &mVAO);
glDeleteBuffers(NUM_BUFFERS, mVBOs);
}
这是Meshes的载体。
std::vector<Vertex> mVertices;
和processNode的标题,它只是创建新的网格物体并将其返回到矢量。
Mesh Model::processMesh(aiMesh *mesh, const aiScene *scene);
答案 0 :(得分:0)
如果在向量中没有保留足够的内存来容纳新元素,则向量可以在添加新元素时重新分配内存。在这种情况下,它会分配一个新的内存扩展,并将旧内存区域中的所有元素复制到新内存中。因此,在将元素复制到新位置之后,将删除旧位置中的元素。成员函数push_back
也可以处理将被复制到向量的临时对象,之后将被删除。
这是一个示范程序
#include <iostream>
#include <vector>
#include <string>
struct A
{
std::string s;
A(const std::string &s) : s(s)
{
std::cout << "Object " << this->s << " is created" << std::endl;
}
A( const A &a ) : s( "copy of " + a.s )
{
std::cout << "A " << this->s << " is created" << std::endl;
}
~A()
{
std::cout << "Object " << s << " is deleted" << std::endl;
}
};
int main()
{
{
std::vector<A> v;
v.push_back(A("A"));
v.push_back(A("B"));
v.push_back(A("C"));
}
}
它的输出可能看起来像
Object A is created
A copy of A is created
Object A is deleted
Object B is created
A copy of copy of A is created
Object copy of A is deleted
A copy of B is created
Object B is deleted
Object C is created
A copy of copy of copy of A is created
A copy of copy of B is created
Object copy of copy of A is deleted
Object copy of B is deleted
A copy of C is created
Object C is deleted
Object copy of copy of copy of A is deleted
Object copy of copy of B is deleted
Object copy of C is deleted