简体   繁体   English

C ++ DirectX无法读取内存

[英]C++ DirectX Unable to read memory

I have a class that haves all my D3D window functions like this: 我有一个具有所有D3D窗口函数的类,如下所示:

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 但是当我调试程序时,它给了我以下错误: 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. 并将其粘贴到Main.cpp中,程序启动时不会崩溃。

Any idea on what might cause the error? 关于什么可能导致错误的任何想法?

On that picture you posted, your "this" pointer is NULL. 在您发布的图片上,“ this”指针为NULL。 Which means you are not initialising your cD3DWindow instance. 这意味着您没有初始化cD3DWindow实例。 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. 它崩溃是因为d3d和d3ddev位于“ this”指针的内存位置,该指针为零。 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. 如果将这两个变量放在main.cpp(全局变量)中,则它们突然会占据适当的内存位置,并且因为您不使用“ this”指针,它也不会崩溃。

Non virtual class methods are just regular functions, which are called with "this" pointer as instance which "owns" the method. 非虚拟类方法只是常规函数,使用“ this”指针作为“拥有”该方法的实例来调用。 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. 如果使用“损坏的此”指针(如null)调用方法,则无法访问该类中的字段,否则它将运行而不会崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM