gpt4 book ai didi

c++ - Assimp 泄漏内存

转载 作者:太空狗 更新时间:2023-10-29 21:39:58 25 4
gpt4 key购买 nike

我最近通过 valgrind 对我的游戏引擎进行了内存泄漏测试;它实际上告诉我,在我的 Mesh 类中有大约 7000 个字节被泄露;奇怪的是它告诉我这个:

7,280 bytes in 1 blocks are definitely lost in loss record 391 of 393
==5639== at 0x4C2C100: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5639== by 0x598647E: ??? (in /usr/lib/libassimp.so.3.0.1264)
==5639== by 0x597F37D: ??? (in /usr/lib/libassimp.so.3.0.1264)
==5639== by 0x58139E5: ??? (in /usr/lib/libassimp.so.3.0.1264)
==5639== by 0x581E2B2: Assimp::Importer::ReadFile(char const*, unsigned int) (in /usr/lib/libassimp.so.3.0.1264)
==5639== by 0x40D71A: glDetail::CMesh::CMesh(char const*) (Mesh.cpp:49)
==5639== by 0x412FB5: _ZN9__gnu_cxx13new_allocatorIN8glDetail5CMeshEE9constructIS2_IRPKcEEEvPT_DpOT0_ (in /home/mattmatt/workspace/C++/alpha++/main-dev/eclipse/Alpha++/Debug/Alpha++)

所以问题是:Assimp 是否应对泄漏的内存负责?这是有问题的代码部分:

CMesh::CMesh(const char* fileName){
Assimp::Importer importer;

const aiScene* scene = importer.ReadFile(fileName, aiProcess_Triangulate |
aiProcess_GenSmoothNormals |
aiProcess_FlipUVs |
aiProcess_CalcTangentSpace
);
if(!scene){
LOG_ERROR("Mesh", "ERROR LOADING MESH ! : CHECK THE SUPPORTED MODEL TYPES MODEL I OR THE FILE PATH !");
abort();
}
const aiMesh* model = scene->mMeshes[0];

std::vector<Vertex> vertices;
std::vector<unsigned int> indices;

const aiVector3D aiZeroVector(.0f, .0f, .0f);
IndexedModel out;

for(unsigned i = 0; i < model->mNumVertices; ++i)
{
const aiVector3D* pPos = &(model->mVertices[i]);
const aiVector3D* pNormal = &(model->mNormals[i]);
const aiVector3D* pTexCoord = model->HasTextureCoords(0) ? &(model->mTextureCoords[0][i]) : &aiZeroVector;

const aiVector3D* pTangent = &(model->mTangents[i]);

Vertex vert (
glm::vec3(pPos->x, pPos->y, pPos->z),///positions
glm::vec2(pTexCoord->x, pTexCoord->y),///UV coords
glm::vec3(pNormal->x, pNormal->y, pNormal->z),///normals
glm::vec3(pTangent->x, pTangent->y, pTangent->z)///tangents

);

vertices.push_back(vert);

out.positions.push_back(*vert.getPos());
out.texCoords.push_back(*vert.getTexCoord());
out.normals.push_back(*vert.getNormal());
out.tangents.push_back(*vert.getTangent());
}
for(unsigned i = 0; i < model->mNumFaces; ++i){
const aiFace& face = model->mFaces[i];
assert(face.mNumIndices == 3);
indices.push_back(face.mIndices[0]);
indices.push_back(face.mIndices[1]);
indices.push_back(face.mIndices[2]);
}


importer.FreeScene();

out.indices = indices;
initMesh(out);
}

可以在这个问题 Memory Leak in opengl Mesh class 查看我的网格类的完整代码如有必要:)

//////////////////////////重要编辑///////////////////////////

我隔离了泄漏资源的代码部分:

Assimp::Importer importer;

const aiScene* scene = importer.ReadFile("res/Suzy.obj", aiProcess_Triangulate |
aiProcess_GenSmoothNormals |
aiProcess_FlipUVs |
aiProcess_CalcTangentSpace
);
if(!scene){
LOG_ERROR("Mesh", "ERROR LOADING MESH ! : CHECK THE SUPPORTED MODEL TYPES MODEL I OR THE FILE PATH !");
abort();
}

我需要更改什么?

最佳答案

我在使用 SDL2 或 opengl 时也遇到过这些错误;您必须假设 assimp 开发人员知道他们在做什么; opengl 也一样,glew和 SDL2。

关于c++ - Assimp 泄漏内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31697590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com