• Advertisement
Sign in to follow this  

[SlimDX] Resource managment quesion

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone,

please examine a code snippet below:

var form = new Form();
form.Width = 640;
form.Height = 480;
var pp = new PresentParameters();
pp.DeviceWindowHandle = form.Handle;
pp.BackBufferWidth = form.Width;
pp.BackBufferHeight = form.Height;

var device = new Device(new Direct3D(), 0, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing, pp);
var swapChain = new SwapChain(device, pp);
Surface surface = swapChain.GetBackBuffer(0);
swapChain.Dispose();

Why the surface resource is still alive even after disposing swap chain? My expectation is that the surface is a part of the swapChain and it should be disposed after the swap chain has been disposed..

Is this pseudo code correct for case with multiple swap chains, i mean is it normal situation when the back buffer surface disposes in each frame?

Device m_device;
void RenderFrame(SwapChain swapChain)
{
Surface surface = swapChain.GetBackBuffer(0);
m_device.SetRenderTarget(0, surface);

// Render 3D scene
surface.Dispose();
}

Share this post


Link to post
Share on other sites
Advertisement
You can use PIX to determine what is happening. I've seen this before but with GDNet. Microsoft says you have to switch to windowed mode before disposing of the swap chain. SetFullscreenState(false, NULL);

The only other thing I can think of is a garbage collection issue.

Run it through PIX to narrow down the problem.

Share this post


Link to post
Share on other sites
In your first code dump you acquire the Surface (Surface surface = swapChain.GetBackBuffer(0);) but don't dispose of it. In COM, whenever you acquire ownership of a handle to an object the reference count is incremented. Now, SlimDX does some trickery in the background to make this easier for you to manage, but in general if you call "GetSomething" in slimdx then you should call "Dispose" on the reference returned when you're done with it.

Share this post


Link to post
Share on other sites
Hello, thanks for answers.

I made some experiment in PIX. My test console application is:

static void Main(string[] args)
{
var mainForm = new Form();
mainForm.Width = 640;
mainForm.Height = 480;
mainForm.Show();
var pp = new PresentParameters();
pp.Windowed = true;
pp.DeviceWindowHandle = mainForm.Handle;
pp.BackBufferWidth = mainForm.Width;
pp.BackBufferHeight = mainForm.Height;
var direct3D = new Direct3D();
var device = new Device(direct3D, 0, DeviceType.Hardware, mainForm.Handle, CreateFlags.HardwareVertexProcessing, pp);
SwapChain swapChain = device.GetSwapChain(0);
for (int i = 0; i < 3; i++)
{
RenderFrame(device, swapChain);
}
swapChain.Dispose();
device.Dispose();
direct3D.Dispose();
}
private static void RenderFrame(Device device, SwapChain swapChain)
{
Surface surface = swapChain.GetBackBuffer(0);
device.SetRenderTarget(0, surface);
device.BeginScene();
device.Clear(ClearFlags.Target, new Color4(1f, 0f, 0f), 0, 1);
device.EndScene();
surface.Dispose();
swapChain.Present(Present.None);
}


and i have got next calls in PIX

24 Frame 2 31695410 2 3027324 330.3
25 <0x08CB65A8> IDirect3DSwapChain9::GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, 0x0019ED3C --> 0x08CB6610) 31706260
26 <0x08CA95E0> IDirect3DDevice9::SetRenderTarget(0x00000000, 0x08CB6610) 31717988
27 <0x08CA95E0> IDirect3DDevice9::BeginScene() 31722974
28 <0x08CA95E0> IDirect3DDevice9::Clear(0x00000000, NULL, 0x00000001, D3DCOLOR_ARGB(0xff,0xff,0x00,0x00), 0.000f, 0x00000001) 31741448
29 <0x08CA95E0> IDirect3DDevice9::EndScene() 31748778
30 <0x08CB6610> IDirect3DSurface9::Release() 31753470
31 <0x08CB65A8> IDirect3DSwapChain9::Present(NULL, NULL, NULL, NULL, 0x00000000) 34130700


What is interesting that the resource <0x08CB6610> is still alive even after call
30 <0x08CB6610> IDirect3DSurface9::Release() 31753470
. So as i see surface.Dispose() actually doesn't free the surface.

Share this post


Link to post
Share on other sites
Dispose calls release. That does not mean that it will free the object, just that if the REFERENCE COUNT is 0 it will free the object.

The back buffer is used by the swap chain. The swap chain maintains a reference to the back buffer. This is just one of many possible references to the back buffer that might exist.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement