2010-04-27 80 views
2

retrieveing緩衝區我遇到的問題是與以下「訪問衝突讀取位置」的煩惱從DirectX的

ID3D10Texture2D *pBackBuffer; 

hr = mpSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*) &pBackBuffer); 

下面是我的代碼...

#include "DX3dApp.h" 




bool DX3dApp::Init(HINSTANCE hInstance, int width, int height) 
{ 
    mhInst = hInstance; 
    mWidth = width; 
    mHeight = height; 

    if(!WindowsInit()) 
    { 
     return false; 
    } 
    if(!InitDirect3D()) 
    { 
     return false; 
    } 

    return true; 
} 

int DX3dApp::Run() 
{ 
    MSG msg = {0}; 

    while (WM_QUIT != msg.message) 
    { 
     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) == TRUE) 
     { 
      TranslateMessage(&msg); 
      DispatchMessage(&msg); 
     } 

     Render(); 
    } 

    return (int) msg.wParam; 
} 

bool DX3dApp::WindowsInit() 
{ 
    WNDCLASSEX wcex; 

    wcex.cbSize = sizeof(WNDCLASSEX); 
    wcex.style   = CS_HREDRAW | CS_VREDRAW; 
    wcex.lpfnWndProc = (WNDPROC)WndProc; 
    wcex.cbClsExtra  = 0; 
    wcex.cbWndExtra  = 0; 
    wcex.hInstance  = mhInst; 
    wcex.hIcon   = 0; 
    wcex.hCursor  = LoadCursor(NULL, IDC_ARROW); 
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
    wcex.lpszMenuName = NULL; 
    wcex.lpszClassName = TEXT("DirectXExample"); 
    wcex.hIconSm  = 0; 
    RegisterClassEx(&wcex); 

    // Resize the window 
    RECT rect = { 0, 0, mWidth, mHeight }; 
    AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE); 

    // create the window from the class above 
    mMainhWnd = CreateWindow(TEXT("DirectXExample"), 
          TEXT("DirectXExample"), 
          WS_OVERLAPPEDWINDOW, 
          CW_USEDEFAULT, 
          CW_USEDEFAULT, 
          rect.right - rect.left, 
          rect.bottom - rect.top, 
          NULL, 
          NULL, 
          mhInst, 
          NULL); 
    if (!mMainhWnd) 
    { 
     return false; 
    } 

    ShowWindow(mMainhWnd, SW_SHOW); 
    UpdateWindow(mMainhWnd); 

    return true; 
} 

bool DX3dApp::InitDirect3D() 
{ 
    DXGI_SWAP_CHAIN_DESC scd; 
    ZeroMemory(&scd, sizeof(scd)); 

    scd.BufferCount = 1; 
    scd.BufferDesc.Width = mWidth; 
    scd.BufferDesc.Height = mHeight; 
    scd.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; 
    scd.BufferDesc.RefreshRate.Numerator = 60; 
    scd.BufferDesc.RefreshRate.Denominator = 1; 
    scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; 
    scd.OutputWindow = mMainhWnd; 
    scd.SampleDesc.Count = 1; 
    scd.SampleDesc.Quality = 0; 
    scd.Windowed = TRUE; 

    HRESULT hr = D3D10CreateDeviceAndSwapChain(NULL,D3D10_DRIVER_TYPE_REFERENCE, 
               NULL, 
               0, 
               D3D10_SDK_VERSION, 
               &scd, 
               &mpSwapChain, 
               &mpD3DDevice); 

    if(!hr != S_OK) 
    { 
     return FALSE; 
    } 

    ID3D10Texture2D *pBackBuffer; 

    hr = mpSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*) &pBackBuffer); 

    return TRUE; 
} 

void DX3dApp::Render() 
{ 

} 


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    switch (message) 
    { 
     // Allow the user to press the escape key to end the application 
     case WM_KEYDOWN: 
      switch(wParam) 
      { 
       // Check if the user hit the escape key 
       case VK_ESCAPE: 
        PostQuitMessage(0); 
       break; 
      } 
     break; 

     // The user hit the close button, close the application 
     case WM_DESTROY: 
      PostQuitMessage(0); 
     break; 
    } 
    return DefWindowProc(hWnd, message, wParam, lParam); 
} 

,我得到以下錯誤

chp1.exe': Unloaded 'C:\Windows\SysWOW64\oleaut32.dll' 
First-chance exception at 0x757ce124 in chp1.exe: Microsoft C++ exception: _com_error at memory location 0x0018eeb0.. 
First-chance exception at 0x757ce124 in chp1.exe: Microsoft C++ exception: _com_error at memory location 0x0018edd0.. 
First-chance exception at 0x757ce124 in chp1.exe: Microsoft C++ exception: _com_error at memory location 0x0018ef1c.. 
The thread 'Win32 Thread' (0xfc4) has exited with code 0 (0x0). 
'chp1.exe': Unloaded 'C:\Windows\SysWOW64\D3D10Ref.DLL' 
First-chance exception at 0x00b71894 in chp1.exe: 0xC0000005: Access violation reading location 0x00000000. 
Unhandled exception at 0x00b71894 in chp1.exe: 0xC0000005: Access violation reading location 0x00000000. 

看來,錯誤發生在最後一個參數。 & pBackBuffer。我添加了這一行代碼併發生錯誤。

[編輯]

如下所述,這個問題是我在訪問一個空指針來緩衝。但我不知道我在做什麼錯了。

回答

3

此錯誤表示您訪問了空指針。最可能的是,mpSwapChain是NULL。

編輯

沒有正確執行你的hr檢查:它應該是if (hr != S_OK)而不是if (!hr != S_OK)

檢查hr的更好方法是:if (FAILED(hr))(或SUCCEEDED(hr)檢查成功時)。這是更好的,因爲hr的一些成功值不是S_OK

+0

這是有道理的。但在同一時間我檢查了人力資源,看看它是否恢復正常。我上面更新了我的代碼。 – numerical25 2010-04-27 13:55:39

+0

啊,很好,現在可以解決這個問題。現在我要找出爲什麼我的mpSwapChain沒有成功初始化 – numerical25 2010-04-27 14:11:38

+0

@ numerical25:嚴重的是,永遠不要嘗試使用'!hr'。無論你認爲它做什麼,它都不會那樣做。 – Alan 2010-04-28 23:28:03

1

找出我的問題。問題是以下行代碼

scd.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; 

我把緩衝區的格式不正確。這幾乎類似

scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; 

所以像interjay說。當這樣的錯誤組成時。更可能的問題是導致SwapChain無法啓動的問題。這更可能是一個邏輯錯誤。

相關問題