c++ - 访问冲突读取位置 0x00184000

标签 c++ c directx

遇到以下行的问题

HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mVB));

CreateBuffer 方法似乎在读取 &mVB 时遇到问题。 mVB 在 box.h 中定义,看起来像这样

ID3D10Buffer* mVB;

下面是完整的代码。这是 mVB 所在的所有文件。

//Box.cpp
#include "Box.h"
#include "Vertex.h"
#include <vector>

Box::Box()
: mNumVertices(0), mNumFaces(0), md3dDevice(0), mVB(0), mIB(0)
{   
}

Box::~Box()
{
    ReleaseCOM(mVB);
    ReleaseCOM(mIB);
}

float Box::getHeight(float x, float z)const
{
    return 0.3f*(z*sinf(0.1f*x) + x*cosf(0.1f*z));
}

void Box::init(ID3D10Device* device, float m, float n, float dx)
{
    md3dDevice = device;
    mNumVertices = m*n;
    mNumFaces = 12;

    float halfWidth = (n-1)*dx*0.5f;
    float halfDepth = (m-1)*dx*0.5f;
    std::vector<Vertex> vertices(mNumVertices);

    for(DWORD i = 0; i < m; ++i)
    {
        float z = halfDepth - (i * dx);
        for(DWORD j = 0; j < n; ++j)
        {
            float x = -halfWidth + (j* dx);

            float y = getHeight(x,z);

            vertices[i*n+j].pos = D3DXVECTOR3(x, y, z);

            if(y < -10.0f)
                vertices[i*n+j].color = BEACH_SAND;
            else if( y < 5.0f)
                vertices[i*n+j].color = LIGHT_YELLOW_GREEN;
            else if (y < 12.0f)
                vertices[i*n+j].color = DARK_YELLOW_GREEN;
            else if (y < 20.0f)
                vertices[i*n+j].color = DARKBROWN;
            else
                vertices[i*n+j].color = WHITE;
        }
    }

    D3D10_BUFFER_DESC vbd;
    vbd.Usage = D3D10_USAGE_IMMUTABLE;
    vbd.ByteWidth = sizeof(Vertex) * mNumVertices;
    vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
    vbd.CPUAccessFlags = 0;
    vbd.MiscFlags = 0;
    D3D10_SUBRESOURCE_DATA vinitData;
    vinitData.pSysMem = &vertices;
    HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mVB));

    //create the index buffer 

    std::vector<DWORD> indices(mNumFaces*3); // 3 indices per face

    int k = 0;

    for(DWORD i = 0; i < m-1; ++i)
    {
        for(DWORD j = 0; j < n-1; ++j)
        {
            indices[k]      = i*n+j;
            indices[k+1]    = i*n+j+1;
            indices[k+2]    = (i*1)*n+j;

            indices[k+3]    = (i*1)*n+j;
            indices[k+4]    = i*n+j+1;
            indices[k+5]    = (i*1)*n+j+1;

            k+= 6;
        }
    }

    D3D10_BUFFER_DESC ibd;
    ibd.Usage = D3D10_USAGE_IMMUTABLE;
    ibd.ByteWidth = sizeof(DWORD) * mNumFaces*3;
    ibd.BindFlags = D3D10_BIND_INDEX_BUFFER;
    ibd.CPUAccessFlags = 0;
    ibd.MiscFlags = 0;
    D3D10_SUBRESOURCE_DATA iinitData;
    iinitData.pSysMem = &indices;
    HR(md3dDevice->CreateBuffer(&ibd, &iinitData, &mIB));
}

void Box::Draw()
{
    UINT stride = sizeof(Vertex);
    UINT offset = 0;
    md3dDevice->IASetVertexBuffers(0, 1, &mVB, &stride, &offset);
    md3dDevice->IASetIndexBuffer(mIB, DXGI_FORMAT_R32_UINT, 0);
    md3dDevice->DrawIndexed(mNumFaces*3, 0 , 0);

}


//Box.h

#ifndef _BOX_H
#define _BOX_H
#include "d3dUtil.h"

盒子.h

class Box {
public:

    Box();
    ~Box();

    void  init(ID3D10Device* device, float m, float n, float dx);
    void  Draw();
    float getHeight(float x, float z)const;

private:
    DWORD mNumVertices;
    DWORD mNumFaces;

    ID3D10Device* md3dDevice;
    ID3D10Buffer* mVB;
    ID3D10Buffer* mIB;
};

#endif

[编辑] 为 m 和 n dx 传递的参数是

m = 129, n = 129, dx = 1.0f(都是 float )

m和n分别代表行和列的透视。 dx 是宽度和高度。

再次感谢您的帮助

最佳答案

如果你想要一个指向 vector 数据数组的指针,你应该使用&vec[0]。如果你使用 &vec,它会给你一个指向 vector 对象的指针,它可能包含各种管理结构/...与你插入 vector 的数据无关。

数据本身存储在从 &vec[0] 开始的连续内存中,因此在初始化 D3D 结构时使用它。

关于c++ - 访问冲突读取位置 0x00184000,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2708985/

相关文章:

c++ - OpenMP C++ : Load imbalance with parallelised for loop

c - 如何在c中聚合字符串数组

使用 x 库编程为 C 编程创建窗口

c++ - DirectX 11 - 点光阴影

windows - 在配备 GeForce 960M 的笔记本电脑上初始化 D3D12 调试接口(interface)失败

c# - 在 C# 中,显示可缩放和平移视频的好方法是什么?

c++ - 在 XCB 事件循环中从线程触发到主线程

c++ - 复制指向对象指针的构造函数

android - cpu-features.h 没有这样的文件或目录 webrtc Android 构建

c++ - 有没有一种方法可以一次声明多个相同类型的对象,并仅通过一个表达式立即用相同的右值对其进行初始化?