You have misinterpreted the problem. The problem isn't that the pointer is local & gets destroyed. The problem is that Assimp::Importer
's destructor destroys what the pointer is pointing to. Since the Assimp::Importer
object gets destroyed at the end of the function, the pointer is now pointing at invalid stuff.
Why do you need a loadData
function? Why not use ReadFile
as suggested here - http://assimp.sourceforge.net/lib_html/usage.html
Alternately, the solution to your problem would be ensure that the Importer
object doesn't go out of scope till your done using the aiScene
.
One possible way may be this - make the Importer
object a parameter to the loadData
method.
const aiScene* IOHandler::loadData(Assimp::Importer & importer,
const std::string& pFile)
{
const aiScene* scene = importer.ReadFile(pFile,
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType);
return scene;
}
The calling code would look like this.
{
........
Assimp::Importer imp;
const aiScene * p = loadData(imp, pFile);
// use aiScene
........
// Importer object goes out of scope here.
}