2011-08-01 52 views
0

程序工作正常(伴隨隨機崩潰)和Memory Validator報告pD3D = Direct3DCreate9中的未初始化讀取問題。 可能是什麼問題?未初始化的讀取問題

init3D.h

class CD3DWindow 
    { 
      public: 
       CD3DWindow(); 
       ~CD3DWindow(); 
       LPDIRECT3D9 pD3D; 
       HRESULT PreInitD3D(); 
       HWND hWnd; 
       bool killed; 
       VOID KillD3DWindow(); 
    }; 

init3D.cpp

CD3DWindow::CD3DWindow() 
    { 
     pD3D=NULL; 
    } 

    CD3DWindow::~CD3DWindow() 
    { 
     if (!killed) KillD3DWindow(); 
    } 

    HRESULT CD3DWindow::PreInitD3D() 
    { 
     pD3D = Direct3DCreate9(D3D_SDK_VERSION); // Here it reports a problem 
     if(pD3D == NULL) return E_FAIL; 
    // Other not related code 

    VOID CD3DWindow::KillD3DWindow() 
    { 
     if (killed) return; 
     diwrap::input.UnCreate(); 
     if (hWnd) DestroyWindow(hWnd); 
     UnregisterClass("D3D Window", wc.hInstance); 
     killed = true; 
    } 

裏面主要的應用程序的.h

CD3DWindow *d3dWin; 

裏面主要的應用程序的.cpp

d3dWin = new CD3DWindow; 
d3dWin->PreInitD3D(); 

這裏是錯誤報告:

Error: UNINITIALIZED READ: reading register ebx 
@0:00:02.969 in thread 4092 

0x7c912a1f <ntdll.dll+0x12a1f> ntdll.dll!RtlUnicodeToMultiByteN 

0x7e42d4c4 <USER32.dll+0x1d4c4> USER32.dll!WCSToMBEx 

0x7e428b79 <USER32.dll+0x18b79> USER32.dll!EnumDisplayDevicesA 

0x4fdfc8c7 <d3d9.dll+0x2c8c7> d3d9.dll!DebugSetLevel 

0x4fdfa701 <d3d9.dll+0x2a701> d3d9.dll!D3DPERF_GetStatus 

0x4fdfafad <d3d9.dll+0x2afad> d3d9.dll!Direct3DCreate9 

0x00644c59 <Temp.exe+0x244c59> Temp.exe!CD3DWindow::PreInitD3D 
c:\_work\Temp\initd3d.cpp:32 
+7

你認爲「隨機崩潰」是「工作正常」? – jalf

+0

:-)沒有。我想建議DirectX正確初始化,所以Direct3DCreate9不是問題。任何想法可能是問題? – en667

+0

誤報,忽略它。在init3D.h中定義了' –

回答

2

編輯:您的堆棧跟蹤是非常,非常strange-的USER32.dll中裏面?這是Windows的一部分。

我可能會建議您將多字節Direct3D與Unicode D3D庫或類似的東西鏈接起來。您不應該能夠導致Windows功能觸發錯誤。

+0

'code bool killed; VOID KillD3DWindow(); ' – en667

+0

in init3D.cpp is defined'code VOID CD3DWindow :: KillD3DWindow() { \t if(killed)return; \t diwrap :: input。UnCreate(); \t \t if(hWnd)DestroyWindow(hWnd); \t UnregisterClass(「D3D Window」,wc.hInstance); \t killed = true; }' – en667

+3

@ en667:您需要將其編輯到問題中。 – Puppy

0

您的Memory Validator應用程序正在向您報告誤報。我會忽略這個錯誤並繼續前進。

0

CD3DWindow類中沒有複製構造函數。這可能不是原因,但它是首先想到的。

如果您的代碼中的任何位置出現任何臨時副本由CD3DWindow實例構成的情況,則該副本的析構函數將銷燬窗口句柄。之後,您的原創將嘗試使用相同的,現在無效的句柄。

對於賦值運算符也是如此。

如果內存還沒有被覆蓋一段時間,這甚至可能起作用。然後突然,內存被重用,你的代碼崩潰了。

所以加入給你的類開始:

private: 
    CD3DWindow(const CD3DWindow&); // left unimplemented intentionally 
    CD3DWindow& operator=(const CD3DWindow&); // left unimplemented intentionally 

如果編譯器抱怨,檢查它指的是代碼。

更新:當然,這個問題可能適用於所有其他類。請閱讀「三條規則」。

+0

由於我得到錯誤C2533:'CD3DWindow :: CD3DWindow':構造函數不允許返回類型(它指向添加的行)。我怎麼能解決這個問題?謝謝。 – en667

+0

糟糕,你是對的。我正在使用複製構造函數的operator =()語法的一部分。我的錯。代碼已更正。 – Sjoerd

+0

謝謝,爲此。 – en667