Raven
Raven

Reputation: 345

C++ DirectX Unable to read memory

I have a class that haves all my D3D window functions like this:

class cD3DWindow
{
public:
    cD3DWindow();
    ~cD3DWindow();

    void initD3D(HWND hWnd);
    void render_frame(void);
    void cleanD3D(void);
private:
    LPDIRECT3D9 d3d;
    LPDIRECT3DDEVICE9 d3ddev;
};

And I use it like this:

void cD3DWindow::initD3D(HWND hWnd)
{
    d3d = Direct3DCreate9(D3D_SDK_VERSION);
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.hDeviceWindow = hWnd;
    d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
    d3dpp.BackBufferWidth = SCREEN_WIDTH;
    d3dpp.BackBufferHeight = SCREEN_HEIGHT;
    d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);
    D3DXCreateFont(d3ddev, 20, 0, FW_EXTRABOLD, 1, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial", &pFont);
}

But when I debug my program It gives me the following error: https://gyazo.com/cb82a94606b2b3c1a49c55b03060d941

It compiles without any error but when I try to start the program it crashes. The program works if i remove the following:

LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;

And pastes it into the Main.cpp the program starts without crashing.

Any idea on what might cause the error?

Upvotes: 0

Views: 211

Answers (1)

Anicka Burova
Anicka Burova

Reputation: 106

On that picture you posted, your "this" pointer is NULL. Which means you are not initialising your cD3DWindow instance. But you are calling it from null pointer. It is crashing because d3d and d3ddev are at memory position from "this" pointer, which is at zero. If you put these two variables in your main.cpp (global variables), suddenly they have a proper memory place and because you don't use "this" pointer it doesn't crash.

Non virtual class methods are just regular functions, which are called with "this" pointer as instance which "owns" the method. If you call a method with "corrupted this" pointer (like null), than you can't access fields in that class, otherwise it will run without a crash.

Upvotes: 2

Related Questions