3

我有一个 DirectX 应用程序。这很简单,但我有一个问题。我创建设备、设备上下文等,一切正常,但是当我退出时,发生崩溃并且错误是:HEAP: Free Heap block 3ad7d18 modified at 3ad7d98 after it was freed. 仅当我IDXGISwapChain Present至少调用一次函数时才会发生这种情况。如果我不这样做,那么整个清洁过程就会顺利进行。此外,我调用Release了每个 COM 对象,并且只有在我释放最后一个 COM 对象时才会发生崩溃(顺序无关紧要)。我在 Windows 7、MS Visual 2012 上使用 DirectX 11 (Win8 SDK)。

我的消息循环功能:

int Engine::run( ){

    MSG msg = { 0 };
    mTimeCounter->restart( ); // doesn't matter
    while( msg.message != WM_QUIT ){ 
        if( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) ){
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        } else {
            updateScene( mTimeCounter->deltaTime( ) );
            drawScene( );  
        }  
    }

    return static_cast<int>( msg.wParam );
}

updateScene 现在什么都不做,绘制场景只调用这两个函数:

void Engine::sceneBegin( ){
    static FLOAT color[] = { 0.05f, 0.15f, 0.05f, 1.0f }; 
    mDeviceContext->ClearRenderTargetView( mBackBufferView, color );
    mDeviceContext->ClearDepthStencilView( mDepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1, 0 );
}


void Engine::sceneEnd( ){
    mSwapChain->Present( 0, 0 ); // crash do not occure if i comment this line of code
}

部分消息切换:

   case WM_QUIT : // i do not receive it even once because i press window's X button and it destroy window before i could receive WM_QUIT ( or not? :P )
    {

                DestroyWindow( mMainWnd );
            }
            break;

            case WM_DESTROY : // i receive it if press window's X button
            {
                PostQuitMessage( 0 ); 
            }  
            break;
return DefWindowProc( hWnd, msg, wParam, lParam );

我初始化并启动引擎的主要功能:

EngTest *eng = new EngTest( );
eng->initialize( hInstance, L"Hi", show );
int r = eng->run( );
delete eng; // crash occures here but only if i call Present at least once.

关闭:

// called in Engine's destructor
void Engine::shutdown( ){ 
    RELEASE_COM( mDepthStencilView );
    RELEASE_COM( mDepthStencilBuffer );
    RELEASE_COM( mBackBufferView );
    RELEASE_COM( mSwapChain );
    if( mDeviceContext )
        mDeviceContext->ClearState( );
    RELEASE_COM( mDeviceContext );
    RELEASE_COM( mDevice );  
}

RELEASE_COM

#define RELEASE_COM( x ) { if( x != NULL ) { x->Release( ); x = NULL; } }
4

2 回答 2

3

好吧...这真的很烦人。这段代码似乎很好,问题出在驱动程序或某事上。当我安装新驱动程序并重新启动 PC 两次时,问题就消失了。

于 2013-05-28T19:44:04.673 回答
0

对于什么是值得的,这就是我解决完全相同问题的方法。Clear()在渲染代码中发生并且仅在D3D11_CREATE_DEVICE_DEBUG打开时发生(增加神秘感)

case WM_CLOSE: // X clicked or Alt+F4
    ::DestroyWindow(hWnd); // triggers WM_DESTROY
    break;
case WM_DESTROY:
    gD3d11Context->ClearState();
    PostQuitMessage(0); // this triggers the WM_QUIT to break the loop
    break;

另外,我的指针包含在自定义ComPtr实现中。它们是类的成员,并以相反的构造顺序自毁。所以没有->Release()堆栈:)

于 2013-09-27T21:13:14.333 回答