CreateBuffer抛出“访问冲突读取位置"; [英] CreateBuffer throwing an "Access violation reading location"

查看:78
本文介绍了CreateBuffer抛出“访问冲突读取位置";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在名为ModelClass的类中,我有一个函数可以执行以下操作:

I have a function, inside a class called ModelClass, that does the following:

bool ModelClass::SetVertices(ID3D11Device* device, VertexType* vertices)
{
    // Error catching variable
    HRESULT result;

    // Setup the vertex buffer description
    D3D11_BUFFER_DESC vertexBufferDesc;
    ZeroMemory(&vertexBufferDesc, sizeof(vertexBufferDesc));
    vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
    vertexBufferDesc.ByteWidth = sizeof(vertices)*24; 
    vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vertexBufferDesc.CPUAccessFlags = 0;
    vertexBufferDesc.MiscFlags = 0;
    //vertexBufferDesc.StructureByteStride = 0;

    // Give the subresource structure a pointer to the vertex data.
    D3D11_SUBRESOURCE_DATA vertexData;
    ZeroMemory(&vertexData, sizeof(vertexData));
    vertexData.pSysMem = vertices;
    vertexData.SysMemPitch = 0;
    vertexData.SysMemSlicePitch = 0;

    // Create the Vertex Buffer
    result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);

    if (FAILED(result))
    {
        return false;
    }
    return true;

} 

运行此命令,给我一条错误消息,指出"Engine.exe中0x00E445A2的未处理异常:0xC0000005:访问冲突读取位置0x00000000.",Visual Studio指向 result = ... 线.对于我的一生,我无法弄清楚为什么这样做.我什至不知道还要发表什么,所以我只列举一些我认为相关的东西:

Running this, gives me an error message saying "Unhandled exception at 0x00E445A2 in Engine.exe: 0xC0000005: Access violation reading location 0x00000000.", with visual studio pointing at the result = ... line. I can not, for the life of me, figure out why it's doing this. I don't even know what else to post, so I'll just enumerate things I find relevant:

ModelClass::ModelClass(ID3D11Device* device, VertexType* vertices, unsigned long* indices)
{
    m_vertexBuffer = 0;
    m_indexBuffer = 0;

    SetVertices(device, vertices);
    SetIndices(device, indices);
}

  • 该类是通过 ModelClass * cube1 = new ModelClass(d3d11Device,v,indexs); 定义的(在全局范围内),并且在上面的代码框中使用了构造函数.
  • li>
  • 在类头文件中将 m_vertexBuffer 变量声明为 ID3D11Buffer * m_vertexBuffer ,并在类构造函数中将其初始化为零.
  • Visual Studio 2013,Windows 8.1 64位上的DirectX 11
  • 如果我将SetVertices函数的内容复制到我的主文件(该文件包含大部分设置)中,则可以立即工作而不会出现此错误.仅当我从类内部调用CreateBuffer时,才会发生这种情况.这与我的索引缓冲区的工作方式完全相同,这表明顶点结构没有问题
  • 该类的所有函数和变量都是公共的
  • 即使将一个,多个或所有参数设置为NULL,CreateBuffer仍会抛出该异常(即,我认为m_vertexBuffer,数据或缓冲区描述不存在问题)
  • 我的主要猜测是,它与类如何使用这些功能有关,但我的代码找不到任何错误
    • The class is being defined with ModelClass* cube1 = new ModelClass(d3d11Device, v, indices);, in the global scope (for now) with the constructor in the code box above.
    • The m_vertexBuffer variable is declared as ID3D11Buffer *m_vertexBuffer in the class header file, and initialized to zero in the class constructor.
    • DirectX 11 on Visual Studio 2013, Windows 8.1 64bit
    • If I copy the contents of the SetVertices function into my main file (which holds most of the setting up), it immediately works without this error. It only happens if I call CreateBuffer from inside the class. This works the exactly the same way for my index buffer, which indicates it's not a problem with the vertex structure
    • All functions and variables of the class are public
    • CreateBuffer still throws that exception even if one, more or all of it's arguments are set to NULL (i.e. I don't think it's a problem with m_vertexBuffer, the data, or buffer description)
    • My main guess is that it has something to do with how classes use these functions, but I can't find anything wrong with my code
    • 请,如果您想查看其余文件,请发表评论,如果有必要,我将粘贴整个内容,这已经使我无所适从了

      Please, if you would like to see the rest of the files, just comment and I will pastebin the entire thing if it's necessary, this is driving me up the wall already

      推荐答案

      绝佳站点, RasterTek .

      无论如何,问题是在初始化 d3d11Device 之前,您正在全局范围内初始化此东西.当程序以任意顺序启动时,将立即创建在全局范围内声明的所有内容.另一方面, d3d11Device 在程序启动时未初始化.

      Anyways, the problem is that you're initializing this thing in the global scope - before d3d11Device is initialized. Everything declared in the global scope is created instantly when the program starts in an arbitrary order. On the other hand, d3d11Device is not initialized when the program starts.

      将其移出全局范围,或从教程中重新添加 Initialize 函数.问题解决了.

      Take it out of the global scope or re-add the Initialize function from the tutorial. Problem solved.

      这篇关于CreateBuffer抛出“访问冲突读取位置";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆