首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >DirectX9中的访问冲突

DirectX9中的访问冲突
EN

Stack Overflow用户
提问于 2011-09-05 11:41:57
回答 2查看 505关注 0票数 1

最简单的说法是:每当我运行代码时,都会抛出一个访问冲突错误。我得出的结论是,在函数System::renderFrame()中调用this->d3ddev.(whatever)是错误的。此函数从第112行开始。如果有人能帮我,那就太好了。(顺便说一句,我已经让它工作了,但我想把这段代码放到类中,这就是我开始遇到麻烦的地方。另外,我之前被告知要确保初始化所有的指针。它们是通过d3d->createDevice()初始化的)

system.h

代码语言:javascript
运行
复制
#ifndef SYSTEM_H
#define SYSTEM_H

#include "stdinc.h"


class System {
  private:
    void initD3D (void);
    void cleanD3D (void);
    void setUpHWND (HINSTANCE, LPSTR, int);
    static LRESULT CALLBACK StaticWindowProc(HWND, UINT, WPARAM, LPARAM);
    LRESULT WindowProc(HWND, UINT, WPARAM, LPARAM);

    HWND window;
    WNDCLASSEX windowClass;
    LPDIRECT3D9 d3d;
    LPDIRECT3DDEVICE9 d3ddev;
    D3DPRESENT_PARAMETERS d3dpp;

    HINSTANCE hInstance;
    LPSTR lpCmdLine;
    int nCmdShow;
  public:
    System (void);
    System (HINSTANCE, LPSTR, int);
    System (const System&);
    ~System (void);

    void renderFrame (void);
};

#endif

system.cpp

代码语言:javascript
运行
复制
#include "system.h"

//////////////////////////////////////////////////
// Class: System
// Private
//////////////////////////////////////////////////

void System::initD3D (void) {
    this->d3d = Direct3DCreate9(D3D_SDK_VERSION);

    ZeroMemory(&(this->d3dpp), sizeof(d3dpp));
    this->d3dpp.Windowed = WINDOWED;
    this->d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    this->d3dpp.hDeviceWindow = this->window;
    this->d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
    this->d3dpp.BackBufferWidth = SCREEN_WIDTH;
    this->d3dpp.BackBufferHeight = SCREEN_HEIGHT;
    this->d3dpp.EnableAutoDepthStencil = TRUE;
    this->d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

    this->d3d->CreateDevice(D3DADAPTER_DEFAULT,
        D3DDEVTYPE_HAL,
        this->window,
        D3DCREATE_SOFTWARE_VERTEXPROCESSING,
        &(this->d3dpp),
        &(this->d3ddev));

    this->d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);
    this->d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
    this->d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);
};

void System::cleanD3D (void) { 
    this->d3d->Release();
    this->d3ddev->Release();
};

void System::setUpHWND (
                        HINSTANCE hInstance,
                        LPSTR lpCmdLine,
                        int nCmdShow) {
    this->hInstance = hInstance;
    this->lpCmdLine = lpCmdLine;
    this->nCmdShow = nCmdShow;

    ZeroMemory(&(this->windowClass), sizeof(WNDCLASSEX));
    this->windowClass.cbSize = sizeof(WNDCLASSEX);
    this->windowClass.style = CS_HREDRAW | CS_VREDRAW;
    this->windowClass.lpfnWndProc = System::StaticWindowProc;
    this->windowClass.hInstance = this->hInstance;
    this->windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    this->windowClass.lpszClassName = "WindowClass";
    RegisterClassEx(&(this->windowClass));

    this->window = CreateWindowEx(NULL, "WindowClass", "The Direct3D Program", 
        WS_OVERLAPPEDWINDOW, SCREEN_X, SCREEN_Y, SCREEN_WIDTH, SCREEN_HEIGHT,
        NULL, NULL, this->hInstance, NULL);
};

LRESULT CALLBACK System::StaticWindowProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    System *SystemPtr = (System*)GetWindowLong(hWnd, GWLP_USERDATA);

    if(SystemPtr)
    {
        return SystemPtr->WindowProc(hWnd, message, wParam, lParam);
    }
    else
    {
       return DefWindowProc(hWnd, message, wParam, lParam);
    }
};

LRESULT System::WindowProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch(message)
    {
    case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        } break;
    }

    return DefWindowProc(hWnd, message, wParam, lParam);
};

//////////////////////////////////////////////////
// Class: System
// Public
//////////////////////////////////////////////////

System::System (void) {
};

System::System (
                HINSTANCE hInstance,
                LPSTR lpCmdLine,
                int nCmdShow) {
    this->setUpHWND(hInstance, lpCmdLine, nCmdShow);

    ShowWindow(this->window, this->nCmdShow);

    this->initD3D();
};

System::System (const System &) {
};

System::~System (void) {
    this->cleanD3D();
};

void System::renderFrame (void) {
    // Update the camera here

    // Update objects

    // Clear objects

    // FOR SOME REASON THERE IS AN ERROR HERE
    this->d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 255, 0), 1.0f, 0);
    this->d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

    this->d3ddev->BeginScene();

    // Draw objects

    // Finish up
    this->d3ddev->EndScene(); 

    this->d3ddev->Present(NULL, NULL, NULL, NULL); 
};

main.cpp

代码语言:javascript
运行
复制
#include "system.h"

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow) {
    System MainSys;
    MainSys = System(hInstance, lpCmdLine, nCmdShow);

    // Enter the main loop
    MSG msg;

    while (TRUE)
    {
        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        if (msg.message == WM_QUIT)
            break;

        MainSys.renderFrame();
    }

    // Clean up DirectX and the COM
    //delete MainSys;

    return msg.wParam;
};
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-05 16:51:02

我尝试编译您的代码,并更改了以下代码行:

代码语言:javascript
运行
复制
System MainSys;
MainSys = System(hInstance, lpCmdLine, nCmdShow);

转到

代码语言:javascript
运行
复制
System MainSys(hInstance, lpCmdLine, nCmdShow);

而且它起作用了。

我承认我不知道它失败的确切原因,但是在您的System类中有很多东西,您正在构造它的两个实例(两行都有一个),然后将一个分配给另一个。这在任何情况下都不是一个好主意,也是不必要的。我猜它与Direct3D引用计数有关,当第二行中的类被复制到MainSys时,你并没有遵守它。

编辑

我刚刚注意到在使用System(hInstance,lpCmdLine,nCmdShow)构造类之后,会调用MainSys的析构函数;因此,刚刚获得的设备将再次释放()。

票数 4
EN

Stack Overflow用户

发布于 2011-09-05 15:49:09

我看不出这里有什么不对劲的地方,但是很容易遗漏一些东西。我建议检查来自每个d3d应用程序接口调用的HRESULTS,以确保没有失败,并遍历assert(指针!= NULL)以尝试捕获问题。或者使用调试器单步执行,并查看变量以确定何时开始失败。

另外,如果您还没有这样做,请打开d3d9调试版本并查看日志。它经常告诉你你错过了什么。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7303713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档