I am following the "3D game programming with directx 11" book (Frank Luna) and I am trying to render a cube. In the code there is this m_device->CreateBuffer() function which is giving me problems.

The exception I get is:


DirectX11 exception in a method which is external to where the CreateDevice() method is called

So I tried to put it in the method where the device is created, and it gives no exception, so I'm wondering if I have to make the m_device something like global or I don't know.

struct Vertex {
        XMFLOAT3 Pos; XMFLOAT4 Color;

 Vertex vertices[] = {
        { XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT4((const float*)&Colors::White)},
        {XMFLOAT3(-1.0f, +1.0f, -1.0f),  XMFLOAT4((const float*)&Colors::Black)},
        {XMFLOAT3(+1.0f, +1.0f, -1.0f),  XMFLOAT4((const float*)&Colors::Red)},
        {XMFLOAT3(+1.0f, -1.0f, -1.0f),  XMFLOAT4((const float*)&Colors::Green)},
        {XMFLOAT3(-1.0f, -1.0f, +1.0f),  XMFLOAT4((const float*)&Colors::Blue)},
        {XMFLOAT3 (-1.0f, +1.0f, +1.0f), XMFLOAT4((const float*)&Colors::Yellow)},
        {XMFLOAT3 (+1.0f, +1.0f, +1.0f), XMFLOAT4((const float*)&Colors::Cyan)},
        {XMFLOAT3 (+1.0f, -1.0f, +1.0f), XMFLOAT4((const float*)&Colors::Magenta)}
    D3D11_BUFFER_DESC vbd;
    vbd.Usage = D3D11_USAGE_IMMUTABLE;
    vbd.ByteWidth = sizeof(Vertex) * 8;
    vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vbd.CPUAccessFlags = 0;
    vbd.MiscFlags = 0;
    vbd.StructureByteStride = 0;
    D3D11_SUBRESOURCE_DATA vinitData;
    vinitData.pSysMem = vertices;
    m_device->CreateBuffer(&vbd, &vinitData, &mBoxVB); //exception is here

I repeat the code perfectly works in the method where I called the CreateDevice() method, but doesn't here, in another method of another class.

  • \$\begingroup\$ Enable the debug layer when you create your device. It will cause diagnostics output to be printed to the output window, which will be useful in narrowing down what you're doing wrong. \$\endgroup\$
    – user1430
    Jan 23 '17 at 18:35
  • \$\begingroup\$ "device was 0xFFFFFFF..." sounds very much like you've corrupted memory someplace. I'd examine the value of "m_device" in the debugger and make sure it's the same pointer returned from your CreateDevice call. \$\endgroup\$
    – user1430
    Jan 23 '17 at 18:36
  • \$\begingroup\$ Josh Petrie♦ hi, how can i examine the value of m_device in the debugger? Do i have to enable it or something \$\endgroup\$
    – Emanuele
    Jan 23 '17 at 18:51
  • \$\begingroup\$ Josh Petrie♦ i mean how can i enable it? Meanwhile i'm searching on the internet \$\endgroup\$
    – Emanuele
    Jan 23 '17 at 18:51
  • 1
    \$\begingroup\$ You say that this code doesn't work because it's "In another method of another class". Is this the same m_device that you created in your other class? Did you pass it to this class? \$\endgroup\$
    – Honeybunch
    Jan 23 '17 at 18:57

m_Device will be of type ID3D11Device*, i.e. it should be a pointer to a Direct3D 11 device object. In almost all Direct3D 11 applications, there is exactly one device, which is initialized with D3D11CreateDevice or a similar function at the start of your program, and uninitialized at the end of it.

All of your 3D objects will usually be created by that device using functions like CreateBuffer. So a lot of times, a global pointer to the device makes sense. In standard notation, the "m_" prefix means that the variable is a member of the class. So if you've created the device in one class, but want to use it in another, the m_device pointer will need to be copied across.

It looks in this case like you've created the device, but either not stored its pointer, or you're using a different device pointer that hasn't been initialized. Perhaps in the constructor of the class that has the problem you can put something like:

ThisClass::ThisClass(ID3D11Device *dev): m_device(dev)

As long as the class instance is initialized after the device was created, and you have the device pointer from D3D11CreateDevice, you can pass it in to the constructor, and the m_device pointer will be correct.

  • 1
    \$\begingroup\$ I'd suggest that if you copy the device you should also AddRef it, then Release it in the destructor. Doing this means that your copy will be valid even if the original is Released. \$\endgroup\$ Feb 6 '17 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.