-2

I have started to learn Direct3D 11 graphics recently and was able to create a simple interface capable of rendering 3D graphics, and them I've tried to organize it in classes, but a problem is already giving me a headache. I have created my ID3D11Device and initialized it with D3D11CreateDeviceAndSwapChain function, but what happens is, right after the function where I initialized it ends, the device goes back to a nullptr value, so when I need it in another parts of the code, it's value is not initialized anymore(this also happens with device context). I have already tried everything I could think of, make it static, declare in global space, pass it's value by reference before it gets reset, declare other functions that uses it in the same function and even set them to be inline, but nothing worked. I really don't know why it loses the value assigned to it, hope someone can help me to solve this. Here's part of the code where the issue happens:

//Code goes here...
hr = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, D3D11_CREATE_DEVICE_DEBUG, NULL, NULL, D3D11_SDK_VERSION, &scd, &pSwapChain, &pDevice, NULL, &pDeviceContext);
// Function goes fine, now pDevice is a valid COM object until the end of the function.
//Code goes here...
//End of the function.
LoadShaders()
pDevice->CreateVertexShader(code here); //Impossible to create vertex shader because pDevice is nullptr again.

Just to be clear my program is initiliazing Direct3D 11 normally, I've already tested it, my only problem is my objects losing data(apparently).

  • Are you passing pDevice into the function by reference, passing it into the function by value, or returning it? – Jerry Jeremiah Mar 11 '21 at 22:40
  • @JerryJeremiah It's currently declared in global space, the function have access to it, and D3D11CreateDeviceAndSwapChain seems to receive it as a pointer, so the value should continue after the end of the function, I don't know why it don't. – Rafael Ferreira Mar 11 '21 at 22:46
  • If it is global and it is being changed then you have a bug somewhere else that is corrupting random bits of memory. If your debugger supports it set a memory breakpoint so you can stop the program whenever that bit of memory changes. – Jerry Jeremiah Mar 11 '21 at 22:47
  • Thank you for the help, I've solved the problem and will answer it here if anyone gets the same problem. – Rafael Ferreira Mar 11 '21 at 23:03
  • BTW, you may want to read [this blog post](https://walbourn.github.io/anatomy-of-direct3d-11-create-device/) – Chuck Walbourn Mar 12 '21 at 03:38
  • @ChuckWalbourn Very useful info. Thank you for recommending this article, I still didn't knew about these cases of incompatibility. – Rafael Ferreira Mar 13 '21 at 01:02

1 Answers1

1

The problem is that ID3D11Device was being assigned a value inside a function of a class named 'D3D', and I was trying to use it in a function of a class named 'Shaders'. After the tip from Jerry Jeremiah on the comments I've used the debugger and noticed the variables had different Class values, when I put all the functions in the same class, the problem was solved.