开发者

Uninitialized read problem

Program works fine (with random crashes) and Memory Validator reports Uninitialized read problem in pD3D = Direct3DCreate9. What could be the problem ?

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;
    }

Inside main app .h

    CD3DWindow *d3dWin;
开发者_如何转开发

Inside main app .cpp

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

And here is the error report:

 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   


Edit: Your stack trace is very, very strange- inside the USER32.dll? That's part of Windows.

What I might suggest is that you're linking the multi-byte Direct3D against the Unicode D3D libraries, or something like that. You shouldn't be able to cause Windows functions to trigger an error.


Your Memory Validator application is reporting false positives to you. I would ignore this error and move on.


There is no copy constructor in your class CD3DWindow. This might not be the cause, but it is the very first thing that comes to mind.

If, by any chance, anywhere in your code a temporary copy is made of a CD3DWindow instance, the destructor of that copy will destroy the window handle. Afterwards, your original will try to use that same, now invalid, handle.

The same holds for the assignment operator.

This might even work, if the memory is not overwritten yet, for some time. Then suddenly, the memory is reused and your code crashes.

So start by adding this to your class:

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

If the compiler complains, check the code it refers to.

Update: Of course, this problem might apply to all your other classes. Please read up on the "Rule of Three".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜