# [.net] Managed DirectX - Resource Management Problems

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

## Recommended Posts

Resource Management is one of the subjects of DirectX which I have never found comprehensive answers for. It is much easier to manage resources in MDX, but I still have some occasional problems which I can never find the solution for. Therfore, I'd like, once and for all, to know how to sort it out. Here are my questions: 1) What are exactly the DX resources which need freeing at the end the life of an application? My guess is all the IDiposable-implementing classes, but not 100% on that. Does Direct3D.Device count as one? 2) What is the best way to implement DX-unmanaged resource freeing at the end of the application? By implementing the IDisposable interface and calling Dispose, or by attaching a function to the Direct3D.Device.Disposing event, or both together? By experience, I have found that the first solution seems the best, but again, I'd like a definite answer on this. Another problem is resource management during and after the device beeing in a Lost state. 3) The MDX Documentation seems to say that the device is aware of it's state, fires the correct Device.DeviceLost and Device.DeviceReset events and calls the Device.Reset on it's own. But I have seen some examples on the net that deal with the lost state by detecting it themselves and attempting to call the Reset function themselves. What is the right way to manage lost states? 4) If I understood correctly, when a Device goes in a lost state, DirectX frees all Pool.Managed resources for us, but we have to free all the Pool.Default on our own if we want the device to be able to restart. But how can we know which resource is which. OK, it is obvious when we create a Mesh under Pool.Managed from what pool it is. But what about Sprite objects for example? (ie: I think I am managing Sprites well, but I keep getting System.NullReferenceException in Sprite.OnLostDevice()). 5) Basically, if you had to create ONE class to manage all these issues, what would it look like? Thanks in adavance to anybody that will take the time to answer my questions.

##### Share on other sites
I have found the solution to one of the problems I was having with my application. I'll post it here so other people who might be in the same case can profit from it.

Problem:
The Control object given to Direct3D.Device or Direct3D.SwapChain is on a Form (or is the form itself). Throughout it's life, that form can be opened/closed several time, without destroying, without destroying it. Therefore, the pointer to the Control given to Device/SwapChain is not altered. But Device.Present/SwapChain.Present still seems to through back a GraphicsException right after closing/opening the form once.

Solution:
Even if the pointer to the Control object given to Direct3D was never altered, it's Handle value was. Every time the parent Form is closed, the Handle is destroyed and every time the parent form is opened, it's recreated. Therefore, if you don't want your application to crash in a very unelegant way, hook functions to the Control's HandleDestroyed and HandleCreated events.

control.HandleDestroyed += new EventHandler(OnControlHandleDestroyed);control.HandleCreated += new EventHandler(OnControlHandleCreated);// ...public void OnControlHandleDestroyed(object sender, EventArgs e){	// Desactivate rendering	bActive = false;}public void OnControlHandleCreated(object sender, EventArgs e){	// Recreate the Device/SwapChain with new PresentParameters	PresentParameters presentParameters = new PresentParameters(oldPresentParameters);	presentParameters.DeviceWindow = (Control)sender;	swapChain.Dispose();	swapChain = new SwapChain(device, presentParameters);	// Reactivate rendering	bActive = true;}

• 9
• 9
• 13
• 41
• 15