• 13
• 18
• 19
• 27
• 9

# CComPtr with ID3D11RenderTargetView

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

## Recommended Posts

Hello,

today I decided to change my raw pointers to CComPtr for my critical DX objects.

CComPtr<IDXGISwapChain> m_swapChain;
CComPtr<ID3D11Device> m_device;
CComPtr<ID3D11DeviceContext> m_deviceContext;
CComPtr<ID3D11RenderTargetView> m_renderTargetView // this doesn't work when used with CComPtr (but works with raw pointers)
CComPtr<ID3D11Texture2D> m_depthStencilBuffer;
CComPtr<ID3D11DepthStencilState> m_depthStencilState;
CComPtr<ID3D11DepthStencilView> m_depthStencilView;
CComPtr<ID3D11RasterizerState> m_rasterState;


for almost every object it works, but for the ID3D11RenderTargetView it crashes on runtime with:

Program: D:\aaa\Debug\aaa.exe
File: c:\program files (x86)\microsoft visual studio 11.0\vc\atlmfc\include\atlcomcli.h
Line: 192

Expression: p==0

I have found over the Internet that this means I am calling method through a smart pointer that was not initialized (source: http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/57c867d6-796d-4f09-aa8e-f529722165db/ )

but actually I don't initialize this object myself. It's initialized by the CreateRenderTargetView() directx method.

Could you explain to me why it doesn't work, please?

Thank you.

##### Share on other sites

How are you handling the case if CreateRenderTarget doesn't succeed? Maybe you'r feeding a nullpointer to CComPtr at some point?

##### Share on other sites

Thanks for the reply.

I have following code:

_result = m_device->CreateRenderTargetView( _backBufferPtr, NULL, &m_renderTargetView );
if( FAILED( _result ) ) {
OutputDebugString( "Failure at m_device->CreateRenderTargetView function in D3DSettings::initialize." );
return false;
}


but it doesn't print the text "Failure..."

after debugging a bit more, I have found out that the runtime error is shown when executing:

OMSetRenderTargets( 1, &m_renderTargetView, m_depthStencilView );

I've checked with debbuger the m_renderTargetView and it is not NULL when achieving this line.

Edited by jajcek

##### Share on other sites

See here:

http://msdn.microsoft.com/en-us/library/aa266799(v=vs.60).aspx

"The operation will assert if p is non-NULL."

It asserts, because it's expecting that it will be modified (thus causing a leak if there is already a value in there).

Maybe you can copy the value into a temporary stack variable of type ID3D11RenderTargetView* in order to pass it to OMSetRenderTargets (which is expecting an array).

##### Share on other sites

Hey phil_t,

Maybe you can copy the value into a temporary stack variable of type ID3D11RenderTargetView* in order to pass it to OMSetRenderTargets (which is expecting an array).

that really worked, thank you, but actually I consider it as a strange workaround.

Edited by jajcek

##### Share on other sites

Well normally the reason you pass the address of a variable to a function is so that it can be modified (which is clearly dangerous in the case of a smart pointer that already holds a value).

The less common reason is for an array, like OMSetRenderTargets - and you were using the (1, &) "trick" to do this while avoiding making a "real" array.

##### Share on other sites
Pass in &m_renderTargetView.p instead of just m_renderTargetView.

The CComPtr class exposes the innards, specifically the pointer it contains (p), for a reason. This is one such example.