optimiz3 / slimdx

Automatically exported from code.google.com/p/slimdx
MIT License
0 stars 0 forks source link

Ancillary ownership in DXGIObject.GetParent<T> #789

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hi,

I just got started using the SlimDX SDK (March 2011) and have discovered some 
unintuitive behavior. I'm using SlimDX's Direct3D 11 interface from a Windows 
Forms application. I need to get the Factory from a D3D11 Device so that I can 
create a swap chain. Since getting the Factory from a Device is somewhat 
tedious, I wrote a method to do it:

// code --------

using Direct3D = SlimDX.Direct3D11;
using DXGI = SlimDX.DXGI;

//...

private static DXGI.Factory GetFactoryFromDevice(Direct3D.Device device)
{
    try
    {
        using (DXGI.Device dxgiDevice = new DXGI.Device(device))
        using (DXGI.Adapter adapter = dxgiDevice.Adapter)
        {
            return adapter.GetParent<DXGI.Factory>();
        }
    }
    catch
    {
        return null;
    }
}

// end code ----

Unfortunately this method doesn't work: the Factory object is already disposed 
before the method returns. A little digging in the source code reveals that 
GetParent creates (if necessary) an object that is, according to the object 
table, ancillary to the object whose parent is requested, which is the Adapter 
in this case.

I can work around this if it's intended, but it's unintuitive that an object's 
parent is ancillary to the object, and it seems like it might be unintentional. 
The parent object that is created has a null Owner and does not have the 
IsAncillary flag set. Also, an ancillary object should not be disposed by the 
user, which is contradicted in the SlimDX Direct3D 11 device creation tutorial. 
See http://slimdx.org/tutorials/devicecreation.php, "DXGI Alt+Enter 
Functionality", where the Factory that is obtained from the SwapChain is indeed 
disposed.

Thanks for reading,

--Paul

Original issue reported on code.google.com by bdebe...@gmail.com on 8 Apr 2011 at 10:52

GoogleCodeExporter commented 8 years ago
I agree that this is weird and not how it should be. However, since SlimDX v1 
is essentially in maintenance mode now, I'm not willing to make a change that 
will break any existing program relying on this behavior. In v2 we're moving 
away from the object table system, so these sorts of issues will disappear.

In the meantime, I've added a Factory property to both D3D10 and D3D11 that 
should make it easy to get the associated factory for a device. You shouldn't 
have to dispose of this property; it will get handled for you. See r2082.

Original comment by Mike.Popoloski on 13 Jun 2011 at 5:05