RenderTarget Textures and disposing when initializing from a Form (not main)

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

Recommended Posts

Hi everyone I have a serious problem disposing my DirextX form. I'm developing a drawing application and i use to debug in windowed mode and then try full screen mode. I'm using a RenderTarget texture as my working area and i can draw successfully in windowed and full screen mode. In my development environment i only run my DirectX form at the Main function, so some simple errors as non-disposed objects when ending my form are not even noticed because the application finishes immediately. My problem begins when i run my DirectX form from another windows form. I need to do some configuration tasks with a Windows form and then use a button to initialize my DirectX form. At this point, if i run my DirectX form in windowed mode, i can draw and finish my application successfully, but if i run it on full screen mode, i can also draw but my DirectX form sometimes refuses to finish and sometimes it finishes but my screen keeps on the same resolution as my DirectX form used to be. Anyway i'm forced to close my original Windows form and then my screen turns back to it's original resolution. No exception is thrown. I think my device is not being disposed correctly, but i have done a lot of things like creating my DirectX form with a new Application Domain and it still hangs at full screen mode. It's interesting that if i don't draw anything then my DirectX form finishes correctly and my screen gets it's original resolution and i can run again my DirectX form from my original Windows form (if i try to run my DirectX form when the error happened then i get an exception when initializing DirectX device). A single draw operation is enough to make my DirectX application unstable when finishing. The lines that do this are the following:
public static void Paint(Sprite Tool, Texture DrawArea, Device dev){
using(Surface DrawAreaSurface=DrawArea.GetSurfaceLevel(0)){
using(Surface DevSurface=dev.GetRenderTarget(0)){
//set texture as rendertarget
dev.SetRenderTarget(0,DrawAreaSurface);

//some drawing with Tool here...

//restore rendertarget
dev.SetRenderTarget(0,DevSurface);
}
}
}


So, if Paint is never used, my DirectX form finishes successfully. From what i have investigated, my Paint function is fine, or at least works fine, but it must be doing something to the device that causes this problem. I know i can put my drawing application on a stand alone exe and call it from my windows form, but a lot of information generated from my windows form is non-serializable so i can't store it on a file and then load it from my drawing application. I'm trying to isolate the execution environment of my DirectX form so i used Application Domains but the same error happened. Any help to solve this problem will be greatly appreciated. Best regards.

Share on other sites
At a guess, there could be unhandled exceptions getting fired during your render to texture, so you could try restoring your original render target within a finally block. So it looks something like this...
public static void Paint(Sprite Tool, Texture DrawArea, Device dev){	using(Surface DevSurface=dev.GetRenderTarget(0)){		using(Surface DrawAreaSurface=DrawArea.GetSurfaceLevel(0)){			//set texture as rendertarget			dev.SetRenderTarget(0,DrawAreaSurface);			try{				//some drawing with Tool here...			}			finally{				//restore rendertarget				dev.SetRenderTarget(0,DevSurface);			}		}	}}
This will insure that your original render target is restored even if there is an exception when rendering to your texture.

Hope this helps,
ViLiO

Share on other sites
Quote:
 Original post by ViLiOAt a guess, there could be unhandled exceptions getting fired during your render to texture, so you could try restoring your original render target within a finally block. So it looks something like this...*** Source Snippet Removed ***This will insure that your original render target is restored even if there is an exception when rendering to your texture.Hope this helps,ViLiO

Hi ViLio, i tried your suggestion but the problem happened again.

Nevertheless i tried a different aproach ten minutes ago. I just reset my device to windowed mode at my dispose function and i'm glad to say that it works!!! Now i can run, end and rerun my DirectX form.

Thanks to everyone.

• 48
• 12
• 10
• 10
• 9
• Forum Statistics

• Total Topics
631380
• Total Posts
2999673
×