Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Problems with 'resetting' device.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 DarkRonin   Members   -  Reputation: 665

Like
0Likes
Like

Posted 01 June 2014 - 07:06 PM

Hi Guys,

I am having a small issue handling 'lost' devices.

The problem is something to do with the surfaces I am using. If I take out all of my surface code (from the entire program), my device handler works fine.

But, I am using surfaces to render my application.

MSDN states that everything that is created on the device ie, surfaces, textures, etc.. need to be released before 'resetting' the device. So, I am handling this like so;

		HRESULT hr=d3dDevice->TestCooperativeLevel();
		if(hr==D3DERR_DEVICELOST)
		{
			Sleep(10);
			return 0;
		}
		else if(hr==D3DERR_DRIVERINTERNALERROR)
			return 133;
		else if(hr==D3DERR_DEVICENOTRESET)
		{
			if(surfaceApplication)
			{
				surfaceApplication->Release();
				surfaceApplication=NULL;
			}
			if(surfaceWindow)
			{
				surfaceWindow->Release();
				surfaceWindow=NULL;
			}
			if(surfaceBackbuffer)
			{
				surfaceBackbuffer->Release();
				surfaceBackbuffer=NULL;
			}

			if(FAILED(d3dDevice->Reset(&d3dpp)))
				return E_FAIL;
		}
I have no geometry or textures loaded in my application. I am just trying to get the renderer to work properly first.

So, I have released everything (except for the device itself) but the handler still returns E_FAIL (unless I get rid of all surface code throught the app).

Any guidance would be awesome smile.png

Sponsor:

#2 L. Spiro   Crossbones+   -  Reputation: 20320

Like
3Likes
Like

Posted 01 June 2014 - 07:34 PM

You likely have other surfaces alive in your executable somewhere. PIX will show you these objects in Direct3D 9.


Instead of trying to keep track of all the objects that need to be released manually (which will soon become impractical to manage and a major waste of time in trying to do so), wrap all the resource objects (textures, surfaces, index buffers, and vertex buffers) behind classes and use the constructor of the class to register that instance with some kind of “release manager” (and unregister in each object’s destructor).

When you want to release all the objects that need to be released just tell the release manager to send said notification to all objects it has and let them release anything they need to release. A similar notification is sent to have those objects recreate their resources when the device comes back.



It is really quite simple. Here is an example.



LSGDirectX9LosableResourceManager.h
	class CDirectX9LosableResourceManager {
	public :
		// == Functions.
		/**
		 * Destroy the losable resource manager.  Should be called when shutting down.
		 */
		static LSVOID LSE_CALL									Destroy();

		/**
		 * Register a resource (also gives the resource a unique ID).
		 *
		 * \param _plrRes The resource to register.  Losable resources call this on
		 *	themselves directly, so this function should never be called by the user.
		 * \return Returns false if a memory error occurred.  If false is returned, the
		 *	engine must shut down.
		 */
		static LSBOOL LSE_CALL									RegisterRes( CDirectX9LosableResource * _plrRes );

		/**
		 * Remove a resource by its ID.
		 *
		 * \param _ui32Id Unique ID of the resource to remove from the list.
		 */
		static LSVOID LSE_CALL									RemoveRes( LSUINT32 _ui32Id );

		/**
		 * Notify all objects that the device has been lost.
		 */
		static LSVOID LSE_CALL									OnLostDevice();

		/**
		 * Notify all objects that the device has been reset.
		 */
		static LSVOID LSE_CALL									OnResetDevice();


	protected :
		// == Members.
		/** List of resources. */
		static std::vector<CDirectX9LosableResource *>						m_vResources;

		/** Unique resource ID. */
		static LSUINT32										m_ui32ResId;

		/** Thread safety. */
		static CCriticalSection									m_csCrit;
	};

LSGDirectX9LosableResource.h
	class CDirectX9LosableResource {
		friend class				CDirectX9LosableResourceManager;
	public :
		// == Various constructors.
		LSE_CALLCTOR				CDirectX9LosableResource();
		LSE_CALLCTOR				~CDirectX9LosableResource();


		// == Functions.
		/**
		 * Must perform some action when the device is lost.
		 */
		virtual LSVOID LSE_CALL			OnDeviceLost() = 0;

		/**
		 * Must renew resources when the device is reset.
		 *
		 * \return Return true if the renewal is successful, false otherwise.
		 */
		virtual LSBOOL LSE_CALL			OnDeviceReset() = 0;


	protected :
		// == Members.
		/** Do we need to reset the resource? */
		LSBOOL					m_bResourceCanBeLost;

	private :
		/** An ID that lets us remove ourselves from the global resource list. */
		LSUINT32				m_ui32UniqueLosableResourceId;
	};

LSGDirectX9LosableResource.cpp
	LSE_CALLCTOR CDirectX9LosableResource::CDirectX9LosableResource() :
		m_bResourceCanBeLost( false ) {
		CDirectX9LosableResourceManager::RegisterRes( this );
	}
	LSE_CALLCTOR CDirectX9LosableResource::~CDirectX9LosableResource() {
		CDirectX9LosableResourceManager::RemoveRes( m_ui32UniqueLosableResourceId );
	}

LSGDirectX9IndexBuffer.h
class CDirectX9IndexBuffer : public CIndexBufferBase, public CDirectX9LosableResource
…
		// == Functions.
		/**
		 * Must perform some action when the device is lost.
		 */
		virtual LSVOID LSE_CALL					OnDeviceLost();

		/**
		 * Must renew resources when the device is reset.
		 *
		 * \return Return true if the renewal is successful, false otherwise.
		 */
		virtual LSBOOL LSE_CALL					OnDeviceReset();
…

LSGDirectX9IndexBuffer.cpp
	LSVOID LSE_CALL CDirectX9IndexBuffer::OnDeviceLost() {
		if ( !m_bResourceCanBeLost || !m_pibIndexBuffer ) { return; }
		// Release the existing index buffer.
		CDirectX9::SafeRelease( m_pibIndexBuffer );
	}

	LSBOOL LSE_CALL CDirectX9IndexBuffer::OnDeviceReset() {
		if ( !m_bResourceCanBeLost ) { return true; }
		return CreateApiIndexBuffer();
	}

CDirectX9LosableResource automatically registers itself with the CDirectX9LosableResourceManager and provides OnDeviceLost() and OnDeviceReset() as pure virtual methods.
CDirectX9IndexBuffer overrides them appropriately.

Because every object that inherits from CDirectX9LosableResource is autmatically registered as a system-wide losable resource, there are never any forgotten objects hanging around causing your device reset to fail.


L. Spiro

#3 DarkRonin   Members   -  Reputation: 665

Like
0Likes
Like

Posted 01 June 2014 - 07:48 PM

Thanks L. Spiro

Yeah, this is all in a class at the moment.

The following are private and all that need releasing (at the end of the app).
 
	// Device
	IDirect3D9* d3dObject;
	IDirect3DDevice9* d3dDevice;
	D3DPRESENT_PARAMETERS d3dpp;
	bool bVSync;

	// Application surface
	IDirect3DSurface9* surfaceApplication;
	IDirect3DSurface9* surfaceWindow;
	IDirect3DSurface9* surfaceBackbuffer;
	RECT DstRect;
So, as it stands I should only have to release the three surfaces in order to reset the device. And if I remove all references to these surfaces, the device does reset happily.

I'll do some more digging though incase I do have a leak somewhere.

Edited by lonewolff, 01 June 2014 - 07:48 PM.


#4 L. Spiro   Crossbones+   -  Reputation: 20320

Like
2Likes
Like

Posted 01 June 2014 - 07:57 PM

Use PIX to determine which of them has an unnecessary reference.


L. Spiro

#5 DarkRonin   Members   -  Reputation: 665

Like
0Likes
Like

Posted 01 June 2014 - 08:14 PM

Thanks again. smile.png

It appears that I do have a memory leak somewhere. Thanks for putting me on the right track smile.png

[edit]
It is related to the surfaces somewhere. Just gotta find it ;)

Edited by lonewolff, 01 June 2014 - 08:21 PM.


#6 Hodgman   Moderators   -  Reputation: 40094

Like
1Likes
Like

Posted 01 June 2014 - 08:36 PM

Double heck the documentation of all your surface-related functions to see which function calls are incrementing the reference counters. The most common cause is that you call a Get function somewhere and don't realize that it has to be paired with a matching Release -- suddenly you're calling Get every frame and the object's ref-count is over 9000!

#7 DarkRonin   Members   -  Reputation: 665

Like
0Likes
Like

Posted 01 June 2014 - 08:48 PM

Double heck the documentation of all your surface-related functions to see which function calls are incrementing the reference counters. The most common cause is that you call a Get function somewhere and don't realize that it has to be paired with a matching Release -- suddenly you're calling Get every frame and the object's ref-count is over 9000!


That could be it you know.

If have a constant 501636 byte leak, when I start the application and then immediately close it.

I have no geometry coded yet, so it just leaves only the surfaces.

I am cleaning up in the end with this. But, obviously missing something somewhere.
 
		if(surfaceApplication)
			surfaceApplication->Release();
		if(surfaceWindow)
			surfaceWindow->Release();
		if(surfaceBackbuffer)
			surfaceBackbuffer->Release();
		if(d3dObject)
			d3dObject->Release();
		if(d3dDevice)
			d3dDevice->Release();
[edit]
Yep, definately to do with surfaces. I have remarked out all surface code and no leak. So, atleast I am headed in the right direction.

Edited by lonewolff, 01 June 2014 - 08:51 PM.


#8 L. Spiro   Crossbones+   -  Reputation: 20320

Like
3Likes
Like

Posted 01 June 2014 - 08:57 PM

the object's ref-count is over 9000!

“lonewolf, what does the PIX say about his reference count?”
It’s over 9,000!!!!


L. Spiro



#9 DarkRonin   Members   -  Reputation: 665

Like
0Likes
Like

Posted 01 June 2014 - 09:13 PM

the object's ref-count is over 9000!

“lonewolf, what does the PIX say about his reference count?”
It’s over 9,000!!!!


L. Spiro


No, that was an example by Hodgman. It isn't over 9000 at all. ;)

#10 Hodgman   Moderators   -  Reputation: 40094

Like
2Likes
Like

Posted 01 June 2014 - 09:15 PM

Also, the Release function returns what the new value of the ref-count is after the call. You can add some assertions in your release-on-lost-device code, checking that those values are zero -- if they're greater than zero, then you've leaked some references somewhere.



#11 DarkRonin   Members   -  Reputation: 665

Like
0Likes
Like

Posted 01 June 2014 - 09:18 PM

Also, the Release function returns what the new value of the ref-count is after the call. You can add some assertions in your release-on-lost-device code, checking that those values are zero -- if they're greater than zero, then you've leaked some references somewhere.


Nice one! Didn't know that smile.png

#12 L. Spiro   Crossbones+   -  Reputation: 20320

Like
0Likes
Like

Posted 01 June 2014 - 09:30 PM

the object's ref-count is over 9000!

“lonewolf, what does the PIX say about his reference count?”
It’s over 9,000!!!!


L. Spiro


No, that was an example by Hodgman. It isn't over 9000 at all. ;)

He was making a reference of his own to this:

That video’s reference count is over 10,000,000!!!!


L. Spiro

#13 DarkRonin   Members   -  Reputation: 665

Like
0Likes
Like

Posted 01 June 2014 - 09:38 PM

Hehe sorry, I didn't get the reference smile.png

[edit]
After some work I traced it down to this and ammended as per the comment

		//This is called every frame	

		if(!FAILED(d3dDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&surfaceBackbuffer)))
		{
			DstRect.left=0;
			DstRect.top=0;
			DstRect.right=nWindowWidth;		
			DstRect.bottom=nWindowHeight;
			
			if(FAILED(d3dDevice->StretchRect(surfaceApplication,NULL,surfaceBackbuffer,&DstRect,D3DTEXF_LINEAR )))
				return 141;
			
			// ADDED THIS TO THE CODE
			if(surfaceBackbuffer)
			{
				surfaceBackbuffer->Release();
				surfaceBackbuffer=NULL;
			}
		}
If anything still looks nasty here, please let me know smile.png

Edited by lonewolff, 01 June 2014 - 10:02 PM.


#14 Hodgman   Moderators   -  Reputation: 40094

Like
0Likes
Like

Posted 01 June 2014 - 10:05 PM

Does StretchRect ever fail? If so, you're not releasing the surface reference.



#15 DarkRonin   Members   -  Reputation: 665

Like
0Likes
Like

Posted 01 June 2014 - 10:12 PM

Does StretchRect ever fail? If so, you're not releasing the surface reference.


True, I'll modify that.

Although if it does fail it is the end of the road for the application. So at this stage it never does seem to fail. But still worth cleaning up that part of code though smile.png

[edit]
Actually that gets released straight after the return call as it gets handled by the destructor immediately after.

Edited by lonewolff, 01 June 2014 - 10:15 PM.


#16 Burnt_Fyr   Members   -  Reputation: 1454

Like
0Likes
Like

Posted 02 June 2014 - 12:33 PM

 

the object's ref-count is over 9000!

“lonewolf, what does the PIX say about his reference count?”
It’s over 9,000!!!!


L. Spiro

 

I would rate this up just on comedic value, but I think that trivializes the ratings system.



#17 cozzie   Members   -  Reputation: 3268

Like
0Likes
Like

Posted 02 June 2014 - 01:55 PM

Don't know if it helps, but you can also use a smart pointer for these d3d variables. I use ccomptr (atlbase.h), that way you dont have to keep track of reference counting and releasing. The only thing I do in my constructor and destructor is set them to NULL. If you do so, make sure you have everything set up right when you 'reuse' an object and try to create something again, without releasing the current data.
Crealysm game & engine development: http://www.crealysm.com
My games: http://www.crealysm.com/games

Looking for a passionate, disciplined and structured producer? PM me




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS