返回指针会扰乱对象(访问冲突)
Returning a pointer messes up the object (access violation)
我在摆弄assimp和C++,我写了一个简单的函数来从文件加载场景。然而,当我试图返回指针时,对象会被搞砸,如果我试图检索成员数据,就会发生访问冲突。这个片段应该演示这个案例:
const aiScene* ResManager::loadScene(const std::string& pFile)
{
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(pFile, aiProcessPreset_TargetRealtime_MaxQuality);
if(!scene)
{
printf("%sn", importer.GetErrorString());
return 0;
}
// If I break the debug here, 'scene' is valid
return scene;
}
void ResManager::loadFromFile(const std::string& pFile)
{
const aiScene* scn = loadScene(pFile);
// If I break the debug here, 'scn' contains gibberish
}
我想我错过了一些关于指针和常量之类的重要内容。有什么想法吗?
当函数结束时,importer
似乎会破坏,因此对象scene
不再指向有效指针。我认为用动态分配importer
Assimp::Importer *importer = new Assimp::Importer;
应该做到这一点。
你应该稍后用摧毁这个物体
delete importer;
相关文章:
- 写入位置0x0000000C时发生访问冲突
- 在 DLL 中嵌入 Python:Py_DECREF列表对象时访问冲突读取位置
- 为什么我的顶点缓冲区对象出现访问冲突错误?
- 尝试创建结构对象,在位置0x3FE00000中创建访问冲突写入错误
- 存储类对象的C++模板链表-未处理的异常访问冲突读取位置
- 访问用于对象的向量时读取冲突
- 创建具有自动存储的自定义对象并推送回 std::vector 时访问冲突
- 使用指向对象实例的指针列表的访问冲突
- 返回指针会扰乱对象(访问冲突)
- MFC:在工作线程中使用对象的成员函数时获取访问冲突错误
- C++对象数组的访问冲突
- C++ xerces 对象的序列化会导致访问冲突
- 对象的访问冲突
- 对象向量中的内存访问冲突
- 创建对象时发生SSE访问冲突崩溃
- 使用对象指针时读取访问冲突
- C++,为什么在修改新分配的对象后会出现访问冲突
- c++类:创建线程的对象+指向函数的指针=访问冲突
- 在初始化列表中创建单例对象会导致访问冲突(仅释放模式)
- 试图读取充满对象的矢量时发生访问冲突