Jump to content
  • Advertisement
Sign in to follow this  
alexisgreene

Crash on exit

This topic is 2049 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I recently had to rewrite my entire game engine and today I just got to rewriting the renderer. I wrote the code to initiate and shutdown D3D11 and decided to test it before continuing. It worked perfectly and closed without a crash. I added some code just to turn the window green to make sure that the renderer was working. It worked but now an exception is raised when I close the application.

This is the exact error that pops up in the debugger:
rdk_client_DEBUG.exe has triggered a breakpoint.

That's all it says, nothing else.


Stepping through with the debugger, it is triggered when IDXGISwapChain::Release() is called. The object itself is in fact valid. I commented out the code to turn the screen green, ran it, and the application closed without any issues.


Here is my shutdown code:

	if(m_pImmediateContext != nullptr)
		m_pImmediateContext->ClearState();

	ReleaseSamplerStates();
	ReleaseConstantBuffers();
	ReleaseRasterizerStates();
	ReleasePixelShaders();
	ReleaseVertexShaders();

	if(m_pDepthStencilTexture != nullptr)
	{
		m_pDepthStencilTexture->Release();
		m_pDepthStencilTexture = nullptr;
	}
	if(m_pDepthStencilView != nullptr)
	{
		m_pDepthStencilView->Release();
		m_pDepthStencilView = nullptr;
	}
	if(m_pRenderTargetView != nullptr)
	{
		m_pRenderTargetView->Release();
		m_pRenderTargetView = nullptr;
	}
	if(m_pSwapChain != nullptr)
	{
		// You may not release a swap chain in full-screen mode because doing so may create
		// thread contention (which will cause DXGI to raise a non-continuable exception).
		// Before releasing a swap chain, first switch to windowed mode
		// (using IDXGISwapChain::SetFullscreenState( FALSE, NULL )) and then call IUnknown::Release.
		m_pSwapChain->SetFullscreenState(FALSE, NULL);

		//**********************************************************
		// THIS IS WHAT CAUSES THE CRASH
		m_pSwapChain->Release();
		//**********************************************************
		m_pSwapChain = nullptr;
	}
	if(m_pImmediateContext != nullptr)
	{
		m_pImmediateContext->Release();
		m_pImmediateContext = nullptr;
	}
	if(m_pDevice != nullptr)
	{
		m_pDevice->Release();
		m_pDevice = nullptr;
	}
	if(m_pAdapter != nullptr)
	{
		m_pAdapter->Release();
		m_pAdapter = nullptr;
	}
	if(m_pFactory != nullptr)
	{
		m_pFactory->Release();
		m_pFactory = nullptr;
	}

 

And here is the code that causes it to crash on exit:

void rdk::render::CRenderD3D::Update(void)
{
    float color[4] = { 0.0f, 1.0f, 0.0f, 1.0f };
	m_pImmediateContext->ClearRenderTargetView(m_pRenderTargetView, color);
	m_pImmediateContext->ClearDepthStencilView(m_pDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0);
	m_pSwapChain->Present(0, 0);
}

 

I am only running in windowed mode right now by the way. Commenting out the call to m_pSwapChain->Present() will allow the application to once again close without a crash.

Why?

[EDIT] If I continue (instead of break) when this happens, the application will continue to exit without any further issues. This is in the debugger of course.

Edited by Deortuka

Share this post


Link to post
Share on other sites
Advertisement

Nothing from D3D debug layer. The only message I get in the output is:

HEAP[rdk_client_DEBUG.exe]: HEAP: Free Heap block 393ae90 modified at 393af48 after it was freed
rdk_client_DEBUG.exe has triggered a breakpoint.

 

This is confusing me quite a bit. How can making a call to IDXGISwapChain::Present() cause this to happen when I release it? Commenting out that Present() call allows the application to close without crashing.

Share this post


Link to post
Share on other sites

Hi!

 

It looks like you are calling released interfaces (Direct3D and Context).

Maybe you would disable the rendering code while exiting the app.

Share this post


Link to post
Share on other sites

Are you calling the shutdown inside a WindowProc handler? That can lead to all kind of weird bugs. Try to set a flag on your close message and handle the D3D shutdown in your message pump.

Share this post


Link to post
Share on other sites

I do not call it from the WindowProc. I specifically call a function that shuts down the renderer and other sub-systems from WinMain(), after the message loop has been canceled (which happens once a WM_QUIT message has been called)

	MSG msg;
	memset(&msg, 0, sizeof(msg));

	// this must be called by the main application thread
	rdk::engine_client::IEngineClient* pEngine = rdk::engine_client::GetEngineClientInterface();
	if(!pEngine->Initiate())
		return 0;

	bool bContinue = true;
	while(bContinue)
	{
		while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
				bContinue = false;
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		pEngine->Update();
	}

	pEngine->ShutDown();
	return static_cast<int>(msg.wParam);

 

The only other code that could possibly be relevant is my start up code. I am in the process of rewriting my engine and stopped to take care of this bug before I finish writing the full renderer. My renderer is literally just start up and shut down code right now. Here is the code I use to start it up (hopefully one of you can catch something that I may be over looking):

	if(!DirectX::XMVerifyCPUSupport())
	{
		if(Params.pErrorLog != nullptr)
			Params.pErrorLog->Print(L"[rdk::render::CRenderD3D::Initiate] DirectX::XMVerifyCPUSupport() failed\n");
		return false;
	};

	if((m_pErrorLog != nullptr) ||
	   (m_pGenericLog != nullptr) ||
	   (m_pWindow != nullptr))
	{
		if(Params.pErrorLog != nullptr)
			Params.pErrorLog->Print(L"[rdk::render::CRenderD3D::Initiate] already initiated\n");
		return false;
	}

	m_pErrorLog = Params.pErrorLog;
	m_pGenericLog = Params.pGenericLog;
	m_pWindow = Params.pWindow;

	assert(m_pErrorLog != nullptr);
	assert(m_pGenericLog != nullptr);
	assert(m_pWindow != nullptr);

	{
		std::wostringstream stream(L"Direct3D ");
		stream << D3D11_MAJOR_VERSION << "." << D3D11_MINOR_VERSION << " (SDK version " << D3D11_SDK_VERSION << ")\n";
		m_pGenericLog->Print(stream.str());
	}

	HRESULT hr;
	hr = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)(&m_pFactory));
	if(FAILED(hr))
	{
		std::wstring str;
		LastDXError(hr, str);
		m_pErrorLog->Print(std::wstring(L"[rdk::render::CRenderD3D::Initiate::CreateDXGIFactory] ") + str + L"\n");
		ShutDown();
		return false;
	}

	hr = m_pFactory->EnumAdapters(0, &m_pAdapter);
	if(hr == DXGI_ERROR_NOT_FOUND)
	{
		std::wstring str;
		LastDXError(hr, str);
		m_pErrorLog->Print(std::wstring(L"[rdk::render::CRenderD3D::Initiate::m_pFactory->EnumAdapters] ") + str + L"\n");
		ShutDown();
		return false;
	}

	DXGI_ADAPTER_DESC Desc;
	hr = m_pAdapter->GetDesc(&Desc);
	if(FAILED(hr))
	{
		std::wstring str;
		LastDXError(hr, str);
		m_pErrorLog->Print(std::wstring(L"[rdk::render::CRenderD3D::Initiate::m_pAdapter->GetDesc] ") + str + L"\n");
		ShutDown();
		return false;
	}

	{
		std::wostringstream stream;
		stream << Desc.Description;
		stream << "\n Dedicated video memory: " << (static_cast<float>(Desc.DedicatedVideoMemory) / (1024 * 1024));
		stream << "\n Dedicated system memory: " << (static_cast<float>(Desc.DedicatedSystemMemory) / (1024 * 1024));
		stream << "\n Shared system memory: " << (static_cast<float>(Desc.SharedSystemMemory) / (1024 * 1024)) << L"\n";
		m_pGenericLog->Print(stream.str());
	}

	D3D_FEATURE_LEVEL featurelevels[] =
	{
		D3D_FEATURE_LEVEL_11_0,
		D3D_FEATURE_LEVEL_10_1,
		D3D_FEATURE_LEVEL_10_0,
	};

	D3D_FEATURE_LEVEL featurelevelcreated;
	UINT flags = 0;
#ifdef _DEBUG
	flags |= D3D11_CREATE_DEVICE_DEBUG;
	if(m_pGenericLog != nullptr)
		m_pGenericLog->Print(L"D3D11_CREATE_DEVICE_DEBUG\n");
#endif

	long	lWindowHeight = 0;
	long	lWindowWidth = 0;
	if(!m_pWindow->GetClientSize(lWindowHeight, lWindowWidth))
	{
		m_pErrorLog->Print(L"[rdk::render::CRenderD3D::Initiate] m_pWindow->GetClientSize() failed\n");
		ShutDown();
		return false;
	}

	DXGI_SWAP_CHAIN_DESC scd;
	memset(&scd, 0, sizeof(DXGI_SWAP_CHAIN_DESC));
	scd.BufferCount							= 1;
	scd.BufferDesc.Width					= lWindowWidth;
	scd.BufferDesc.Height					= lWindowHeight;
	scd.BufferDesc.Format					= DXGI_FORMAT_R8G8B8A8_UNORM;
	scd.BufferDesc.RefreshRate.Numerator	= 60;
	scd.BufferDesc.RefreshRate.Denominator	= 1;
	scd.BufferUsage							= DXGI_USAGE_RENDER_TARGET_OUTPUT;
	scd.OutputWindow						= m_pWindow->GetHWND();
	scd.SampleDesc.Count					= 1;
	scd.SampleDesc.Quality					= 0;
	scd.Windowed							= Params.bWindowed;

	hr = D3D11CreateDeviceAndSwapChain(m_pAdapter,
									   D3D_DRIVER_TYPE_UNKNOWN,
									   NULL,
									   flags,
									   featurelevels,
									   sizeof(featurelevels) / sizeof(featurelevels[0]),
									   D3D11_SDK_VERSION,
									   &scd,
									   &m_pSwapChain,
									   &m_pDevice,
									   &featurelevelcreated,
									   &m_pImmediateContext);
	if(FAILED(hr))
	{
		std::wstring str;
		LastDXError(hr, str);
		m_pErrorLog->Print(std::wstring(L"[rdk::render::CRenderD3D::Initiate::D3D11CreateDeviceAndSwapChain] ") + str + L"\n");
		ShutDown();
		return false;
	}

	// check which version was created
	switch(featurelevelcreated)
	{
	case D3D_FEATURE_LEVEL_11_0:
		m_pGenericLog->Print(L"D3D_FEATURE_LEVEL_11_0\n");
		break;
	case D3D_FEATURE_LEVEL_10_1:
		m_pGenericLog->Print(L"D3D_FEATURE_LEVEL_10_1\n");
		break;
	case D3D_FEATURE_LEVEL_10_0:
		m_pGenericLog->Print(L"D3D_FEATURE_LEVEL_10_0\n");
		break;
	default:	// unsupported version created
		m_pErrorLog->Print(L"[rdk::render::CRenderD3D::Initiate] failed to create a supported freature level\n");
		return false;
	}

	// create the back buffer
	ID3D11Texture2D *pBuffer = nullptr;
	hr = m_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&pBuffer));
	if(FAILED(hr))
	{
		std::wstring str;
		LastDXError(hr, str);
		m_pErrorLog->Print(std::wstring(L"[rdk::render::CRenderD3D::Initiate::m_pSwapChain->GetBuffer] ") + str + L"\n");
		ShutDown();
		return false;
	}
	hr = m_pDevice->CreateRenderTargetView(pBuffer, NULL, &m_pRenderTargetView);
	pBuffer->Release();
	if(FAILED(hr))
	{
		std::wstring str;
		LastDXError(hr, str);
		m_pErrorLog->Print(std::wstring(L"[rdk::render::CRenderD3D::Initiate::m_pDevice->CreateRenderTargetView] ") + str + L"\n");
		ShutDown();
		return false;
	}

	// create the depth stencil texture
	D3D11_TEXTURE2D_DESC td;
	memset(&td, 0, sizeof(td));
	td.Width				= lWindowWidth;
	td.Height				= lWindowHeight;
	td.MipLevels			= 1;
	td.ArraySize			= 1;
	td.Format				= DXGI_FORMAT_D24_UNORM_S8_UINT;
	td.SampleDesc.Count		= 1;
	td.SampleDesc.Quality	= 0;
	td.Usage				= D3D11_USAGE_DEFAULT;
	td.BindFlags			= D3D11_BIND_DEPTH_STENCIL;
	td.CPUAccessFlags		= 0;
	td.MiscFlags			= 0;

	hr = m_pDevice->CreateTexture2D(&td, NULL, &m_pDepthStencilTexture);
	if(FAILED(hr))
	{
		std::wstring str;
		LastDXError(hr, str);
		m_pErrorLog->Print(std::wstring(L"[rdk::render::CRenderD3D::Initiate::m_pDevice->CreateTexture2D] ") + str + L"\n");
		ShutDown();
		return false;
	}

	// create the depth stencil view
	D3D11_DEPTH_STENCIL_VIEW_DESC dsvd;
	memset(&dsvd, 0, sizeof(dsvd));
	dsvd.Format				= td.Format;
	dsvd.ViewDimension		= D3D11_DSV_DIMENSION_TEXTURE2D;
	dsvd.Texture2D.MipSlice	= 0;
	hr = m_pDevice->CreateDepthStencilView(m_pDepthStencilTexture, &dsvd, &m_pDepthStencilView);
	if(FAILED(hr))
	{
		std::wstring str;
		LastDXError(hr, str);
		m_pErrorLog->Print(std::wstring(L"[rdk::render::CRenderD3D::Initiate::m_pDevice->CreateDepthStencilView] ") + str + L"\n");
		ShutDown();
		return false;
	}
	m_pImmediateContext->OMSetRenderTargets(1, &m_pRenderTargetView, m_pDepthStencilView);

	// set the viewport
    D3D11_VIEWPORT vp;
    vp.Width	= static_cast<float>(lWindowWidth);
    vp.Height	= static_cast<float>(lWindowHeight);
    vp.MinDepth	= D3D11_MIN_DEPTH; // 0.0f;
    vp.MaxDepth	= D3D11_MAX_DEPTH; // 1.0f;
    vp.TopLeftX	= 0;
    vp.TopLeftY	= 0;
    m_pImmediateContext->RSSetViewports(1, &vp);

	// create rasterize states
	if(!CreateRasterizerStates())
	{
		m_pErrorLog->Print(L"[rdk::render::CRenderD3D::Initiate] CreateRasterizerStates() failed\n");
		ShutDown();
		return false;
	}
	m_pImmediateContext->RSSetState(m_RasterizerStates[DefaultRasterizerState]);

	// create the constant buffers
	if(!CreateConstantBuffers())
	{
		m_pErrorLog->Print(L"[rdk::render::CRenderD3D::Initiate] CreateConstantBuffers() failed\n");
		ShutDown();
		return false;
	}

	// create all sampler states
	if(!CreateSamplerStates())
	{
		m_pErrorLog->Print(L"[rdk::render::CRender::Initiate] CreateSamplerStates() failed\n");
		ShutDown();
		return false;
	}

	// load vertex shaders
	if(!LoadVertexShaders())
	{
		m_pErrorLog->Print(L"[rdk::render::CRender::Initiate] LoadVertexShaders() failed\n");
		ShutDown();
		return false;
	}

	// load pixel shaders
	if(!LoadPixelShaders())
	{
		m_pErrorLog->Print(L"[rdk::render::CRender::Initiate] LoadPixelShaders() failed\n");
		ShutDown();
		return false;
	}

	// set the default world transformation matrix
	SetWorldMatrix(DirectX::XMMatrixIdentity());

	// attach window callbacks
	if(!m_pWindow->AddCallback(WM_SIZE, Size))
	{
		m_pErrorLog->Print(L"[rdk::render::CRenderD3D::Initiate] Window.AddCallback(WM_SIZE) failed\n");
		ShutDown();
		return false;
	}

	return true;

Share this post


Link to post
Share on other sites
I am not sure if this may or may not be related, but the same thing happens when I play PlanetSide 2 (which is a UDK game I believe). The game runs fine but crashes on exit. There are many other people who complain about this issue in the forums for that game's website. Maybe I have the same problem that the developer's of that game have? Maybe this is an NVIDIA driver bug?

http://forums.station.sony.com/ps2/index.php?threads/planetside-2-crash-when-exit-game.48970/

[EDIT]
In order to test my NVIDIA driver bug theory, I did a fresh compile and ran it through the debugger. It crashed on exit. I made a copy of the binaries, one copy set to run on the default Intel graphics card and the other copy set to run on the NVIDIA graphics card. I ran both copies outside the debugger and both closed without a crash.

It seems that it only crashes on exit when running from the debugger. Maybe this is a bug with the VS2012 debugger? Edited by Deortuka

Share this post


Link to post
Share on other sites
After you receive a quit message in your message loop, you dispatch it which starts closing your window THEN you try to update your engine one more time before exiting which calls the present method but there is probably no window to present to since the quit message was dispatched. Don't update your engine after you dispatch a quit message. Use an if statement, so if a quit message is received, quit the program, otherwise if bContinue is still true update the engine. This is a bug in your code, so sometimes it crashes and sometimes not. Probably depends on if your engine is fast enough to call present one more time before the window is closed, which is why it might work in a release build and not a debug build

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!