Archived

This topic is now archived and is closed to further replies.

robert_s

A D3D Object Has Non-Zero Reference Count

Recommended Posts

Hi All. When aI close my app I get this message: A D3D Object Has Non-Zero Reference Count (meaning things were not properly cleaned up). This sample will now exit. I have created teh app by using DirectX9 Wizard and added my own skybox class. Now in my main program I am passing a pointer of the device to skybox object. Like below: HRESULT CMainApp::InitDeviceObjects() { // pass device to Skybox m_SkyBox.InitDeviceObjects( m_pd3dDevice); return S_OK; } Then in Skybox class (see below) class CSkyBox { public: CSkyBox(); //default constructor private: LPDIRECT3DDEVICE9 m_pd3dDevice; }; I receive this pointer and assign to a local copy (declared in skybox in private section) HRESULT CSkyBox::InitDeviceObjects(LPDIRECT3DDEVICE9 pd3dDeviceIn) { m_pd3dDevice = pd3dDeviceIn; return S_OK; } Now. When I close my app I get the message (mentioned at the top). I know that is something to do with destroying device (m_pd3dDevice in skybox). I dont understand why Device would increment if I did not create one apart from teh one in main program. In sky box I am just trying to point at the device from main, so if it gets destroyed then the one in skybox as its pointing to it should be destroeyd too. Do you know how to solve this problem? Do I need to destroy m_pd3dDevice in skybox. I even put this local m_pd3dDevice into skybox destructor. Still something wrong!

Share this post


Link to post
Share on other sites
Have you determined that calling m_SkyBox.InitDeviceObjects is what caused the resource leak? If you comment out that line, do you still get the message?

Is CMainApp properly calling Release() on its D3D device object?

You should call m_pd3dDevice->AddRef() in CSkyBox::InitDeviceObjects, then call m_pd3dDevice->Release() and NULLify m_pd3dDevice in CSkyBox''s destructor or cleanup function. This is a good practice because you prevent m_pd3dDevice from being released prematurely, before CSkyBox is done with it.

Share this post


Link to post
Share on other sites
CMainApp is inherited from class CD3DApplication (like this -> CMainApp : public CD3DApplication) This app was generated by DX9 wizard and when I checked d3dapp.cpp I could see that the device is released properly in CD3DApplication class. Well it was writtten by microsoft so I assume they know what they''ve done. This means that I dont declare/destroy m_pd3dDevice as its already inherited from the Microsoft class which is surely porperly cleaned on program exit.

I did comment out in skybox the following line :
m_pd3dDevice = pd3dDeviceIn;
and I dont get this leak. But I must use this pointer as I want to render my skybox in my SkyBox::Render() function so I must point to the device from main app. Still not sure how to solve it!


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
do you have the DX debugging runtime installed? if so, start it up via the DX control panel applet and then re-run your app in debug mode with no breakpoints set. when you close down your app the debug spew should have a list of the unreleased alloc IDs. you can then go back into the control panel applet and tell it to break on one of those alloc IDs. then go back and re-run your app again in debug mode and when that object is allocated the debugger will stop the app. this will tell you which object it is that you need to release before the device itself is released.

Share this post


Link to post
Share on other sites
Sorry Anonymous Poster. When you say DX debugging runtime, do you mean DirectX Error Lookup tool or there is some other tool you suggest to use?

When I close my app I get this error:

Unhandled exception : 0xC0000005 access violation.

I pasted "0xC0000005" into DX Error Lookup and got this message

HRESULT: 0x00000005 (5)
Name: ERROR_ACCESS_DENIED
Description: Access is denied.
Severity code: Success
Facility Code: FACILITY_NULL (0)
Error Code: 0x0005 (5)


I am sure I am not deleting deveice properly as its says (see first my post at the top) that the reference count is not zero so I am not deleting this stuff properly. But why the device is incremented if I am only creating it once. In skybox I am just trying to point at the device which originally is created in main not in skybox. arghhh.. This implementation is ok in DirectX 8 but in 9 there is a problem. Anyone has any suggestions how to solve it?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
no, i''m talking about this: http://www.microsoft.com/downloads/details.aspx?familyid=fb6b635e-9f47-43d7-8e4e-11de9356849f&languageid=f49e8428-7071-4979-8a67-3cffcb0c2524&displaylang=en

Share this post


Link to post
Share on other sites
Its ok. I solved my problem.
The solution was to set the local device in skybox to NULL as in SkyBox::InitiDeviceObjects I point with the local device at the main device in Main. Now, when the main device gets destroyed the local must be set to NUL otherwise the reference count is held by local device because it thinks it is still pointing at the main device which isnt true as the main one is already destroyed. Thats why the app complains when exiting.
Thanks guys for your help anyway!

Share this post


Link to post
Share on other sites