netrunner
netrunner

Reputation: 55

Segmentation fault using IDXGIAdapter

I am reading through the relevant Microsoft docs and found this example of how to create IDGI data struct then use it to read memory size but I get Segmentation fault

#include <iostream>
#include <d3d9.h>
#include <D3D9Types.h>

LPDIRECT3D9       g_pDirect3D = NULL;
LPDIRECT3DDEVICE9 g_pDirect3D_Device = NULL;


int main(void)
{
    UINT x = 0; // Ordinal number that denotes the display adapter. 
    DWORD xWord = 0 ; 
    D3DADAPTER_IDENTIFIER9 pIdentifier ;
    g_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION);

    HRESULT hResult = g_pDirect3D->GetAdapterIdentifier(x, xWord, &pIdentifier);
    IDXGIDevice * pDXGIDevice;
    HRESULT hr  = g_pDirect3D->QueryInterface(__uuidof(IDXGIDevice), (void **)&pDXGIDevice);
    IDXGIAdapter * pDXGIAdapter;
    
    pDXGIDevice->GetAdapter(&pDXGIAdapter); // segfault at this line

    return 0;
}

every thing works perfectly but when I uncomment the line before return 0; I get the error

Microsoft Basic Render Driver
Segmentation fault

why do I get that error

Upvotes: 0

Views: 131

Answers (1)

Simon Mourier
Simon Mourier

Reputation: 138906

You can't get a IDXGIDevice reference from a IDirect3D9 reference using QueryInterface.

The return HRESULT from the QueryInterface call is probably 0x80004002 (E_NOINTERFACE), so pDXGIDevice is null and the following calls cause a crash. You should always check HRESULT values.

Otherwise, for new developments, you should forget about DirectX9 and use DirectX11 or DirectX12 (and this will enable DXGI).

Upvotes: 1

Related Questions