在动态分配的类中写入指针变量时访问冲突

时间:2011-02-24 19:25:03

标签: c++ memory-management access-violation

好的,所以标题不能很好地解释我的情况所以我会在这里尝试解释一下:

这是我的班级结构的一部分:

ObjectView (abstract class)

ShipView : ObjectView (child of object view)

在方法中,我创建了一个新的ShipView:

ShipView *shipview (in header file).

shipview = new ShipView(in main part of code).

然后我运行shipview->Initialise(); 在新课程中设置一切。

但是当我到达试图写入ObjectView类中声明的指针的任何代码行时,它将不允许我这样做并给我一个访问冲突消息。 我收到的消息如下:

"Unhandled exception at 0x00a0cf1c in AsteroidGame.exe: 0xC0000005: Access violation writing location 0xbaadf011."

例如这一行:

_ObjectData = new Model[mesh->mNumVertices];

会给我错误。

我刚把它放在头文件中:

struct Model{
    GLfloat x,y,z;
    GLfloat nX,nY,nZ;
    GLfloat u,v;
};

Model *_ObjectData;

但是,如果我要按照

的方式做一些事情
Model *_ObjectData = new Model[mesh->mNumVertices];

(一次声明并初始化)

它会起作用....

就好像它不知道头文件在那里,或者类没有正确构造,因此没有正确分配内存。

非常感谢任何帮助。

修改 的 头文件:

class ObjectView
{
public:
    ObjectView(void);
    virtual ~ObjectView(void);

    void Initialise(std::string objectpath, std::string texturepath);
    void InitialiseVBO(const aiScene* sc);  

    void RenderObject();

    virtual void ScaleObject() = 0;
    virtual void TranslateObject() = 0;
    virtual void RotateObject() = 0;

protected:
    struct Model{
        GLfloat x,y,z;
        GLfloat nX,nY,nZ;
        GLfloat u,v;
    };
    Model *_ObjectData;

    struct Indices{
        GLuint x,y,z;
    };
    Indices *_IndicesData;  

    TextureLoader _textureloader;
    GLuint _objectTexture;

    GLuint _objectVBO;
    GLuint _indicesVBO;

    int _numOfIndices;
};

代码:

void ObjectView::InitialiseVBO(const aiScene* sc)
{   
    const aiMesh* mesh = sc->mMeshes[0];

    _ObjectData = new Model[mesh->mNumVertices];

    for(unsigned int i = 0; i < mesh->mNumVertices; i++)
    {
        _ObjectData[i].x = mesh->mVertices[i].x;
        _ObjectData[i].y = mesh->mVertices[i].y;
        _ObjectData[i].z = mesh->mVertices[i].z;

        _ObjectData[i].nX = mesh->mNormals[i].x;
        _ObjectData[i].nY = mesh->mNormals[i].y;
        _ObjectData[i].nZ = mesh->mNormals[i].z;

        _ObjectData[i].u = mesh->mTextureCoords[0][i].x;
        _ObjectData[i].v = 1-mesh->mTextureCoords[0][i].y;
    }

    glGenBuffers(1, &_objectVBO);

    glBindBuffer(GL_ARRAY_BUFFER, _objectVBO);
        glBufferData(GL_ARRAY_BUFFER, sizeof(Model) * mesh->mNumVertices, &_ObjectData[0].x, GL_STATIC_DRAW);

    _IndicesData = new Indices[mesh->mNumFaces];

    for(unsigned int i = 0; i < mesh->mNumFaces; ++i)
    {
        for (unsigned int a = 0; a < 3; ++a)
        {
            unsigned int temp = mesh->mFaces[i].mIndices[a];
            if(a == 0)
                _IndicesData[i].x = temp;
            else if(a == 1)
                _IndicesData[i].y = temp;
            else
                _IndicesData[i].z = temp;
        }
    }

    glGenBuffers(1, &_indicesVBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indicesVBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices) * mesh->mNumFaces, _IndicesData, GL_STATIC_DRAW);

    _numOfIndices = sizeof(Indices) * mesh->mNumFaces;

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    delete _ObjectData;
    delete _IndicesData;
}

2 个答案:

答案 0 :(得分:0)

0xbaadf011可能是0xbaadf00d的偏移量,它是Bad Food的十六进制同义词。它肯定是一个未初始化的指针。

在使用它们之前,您显然没有设置指针。我建议在崩溃时查看调用堆栈,在崩溃上方的某个函数上设置断点,然后重新启动程序并逐行运行,直到找到设置为0xbaadf011或0xbaadf00d的指针。然后弄清楚你应该在哪里设置指针。

答案 1 :(得分:0)

如果

_ObjectData = new Model[mesh->mNumVertices];

时崩溃

Model *_ObjectData = new Model[mesh->mNumVertices];

没有,看起来你的对象看起来没有被初始化 我的通灵调试器建议你声明了一个不同的变量,也称为shipview,这就是你所谓的Initialize()