Why does calling CreateDXGIFactory prevent my program from exiting?

Posted by smoth190 on Game Development See other posts from Game Development or by smoth190
Published on 2012-04-01T17:28:20Z Indexed on 2012/04/01 23:40 UTC
Read the original article Hit count: 324

Filed under:
|

I'm using CreateDXGIFactory to get the graphics adapters and display modes. When I call it, it works fine and I get all the data. However, when I exit my program, the main Win32 thread exits, but something stays open because it keeps debugging. Does CreateDXGIFactory create an extra thread and I'm not closing it? I don't understand. The only thing I would suspect is that in the documentation it says it doesn't work if it's called from DllMain. It is in a DLL, but it's not called from DllMain. And it doesn't fail, either.

I'm using DirectX 11.

Here is the function that initializes DirectX. I haven't gotten past retrieving the refresh rate because of this problem. I commented everything out to pinpoint the problem.

bool CGraphicsManager::InitDirectX(HWND hWnd, int width, int height)
    {
        HRESULT result;

        IDXGIFactory* factory;
        IDXGIOutput* output;
        IDXGIAdapter* adapter;
        DXGI_MODE_DESC* displayModes;
        DXGI_ADAPTER_DESC adapterDesc;

        unsigned int modeCount = 0;
        unsigned int refreshNum = 0;
        unsigned int refreshDen = 0;

        //First, we need to get the monitors refresh rater
        result = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&factory);
        //if(FAILED(result))
        //{
            //MemoryUtil::MessageBoxError(TEXT("InitDirectX"), 0, 0, TEXT("Failed to create DXGI factory\nError:\n%s"), DXGetErrorDescription(result));
            //return false;
        //}

        /*//Create a graphics card adapter
        result = factory->EnumAdapters(0, &adapter);
        if(FAILED(result))
        {
            MemoryUtil::MessageBoxError(TEXT("InitDirectX"), 0, 0, TEXT("Failed to get graphics adapters\nError:\n%s"), DXGetErrorDescription(result));
            return false;
        }

        //Get the output
        result = adapter->EnumOutputs(0, &output);
        if(FAILED(result))
        {
            MemoryUtil::MessageBoxError(TEXT("InitDirectX"), 0, 0, TEXT("Failed to get adapter output\nError:\n%s"), DXGetErrorDescription(result));
            return false;
        }

        //Get the modes
        result = output->GetDisplayModeList(DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_INTERLACED, &modeCount, 0);
        if(FAILED(result))
        {
            MemoryUtil::MessageBoxError(TEXT("InitDirectX"), 0, 0, TEXT("Failed to get mode count\nError:\n%s"), DXGetErrorDescription(result));
            return false;
        }

        displayModes = new DXGI_MODE_DESC[modeCount];
        result = output->GetDisplayModeList(DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_INTERLACED, &modeCount, displayModes);
        if(FAILED(result))
        {
            MemoryUtil::MessageBoxError(TEXT("InitDirectX"), 0, 0, TEXT("Failed to get display modes\nError:\n%s"), DXGetErrorDescription(result));
            return false;
        }

        //Now we need to find one for our screen size
        for(unsigned int i = 0; i < modeCount; i++)
        {
            if(displayModes[i].Width == (unsigned int)width)
            {
                if(displayModes[i].Height == (unsigned int)height)
                {
                    refreshNum = displayModes[i].RefreshRate.Numerator;
                    refreshDen = displayModes[i].RefreshRate.Denominator;
                    break;
                }
            }
        }

        //Store the video card data
        result = adapter->GetDesc(&adapterDesc);
        if(FAILED(result))
        {
            MemoryUtil::MessageBoxError(TEXT("InitDirectX"), 0, 0, TEXT("Failed to get adapter description\nError:\n%s"), DXGetErrorDescription(result));
            return false;
        }
        m_videoCard = new CVideoCard();
        MemoryUtil::CreateGameObject(m_videoCard);

        m_videoCard->VideoCardMemory = (unsigned int)(adapterDesc.DedicatedVideoMemory);
        wcstombs_s(0, m_videoCard->VideoCardDescription, 128, adapterDesc.Description, 128);*/

        //ReleaseCOM(output);
        //ReleaseCOM(adapter);
        ReleaseCOM(factory);
        //DeletePointerArray(displayModes);

        return true;
    }

Also, I don't know if this means anything, but this is some of the output log when the function is commented out:

//...
'LostRock.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
'LostRock.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll', Cannot find or open the PDB file
'LostRock.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll', Cannot find or open the PDB file
'LostRock.exe': Loaded 'C:\Windows\SysWOW64\uxtheme.dll', Cannot find or open the PDB file
'LostRock.exe': Loaded 'C:\Program Files (x86)\Common Files\microsoft shared\ink\tiptsf.dll', Cannot find or open the PDB file
'LostRock.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll', Cannot find or open the PDB file
'LostRock.exe': Loaded 'C:\Windows\SysWOW64\oleaut32.dll', Cannot find or open the PDB file
'LostRock.exe': Loaded 'C:\Windows\SysWOW64\clbcatq.dll', Cannot find or open the PDB file
'LostRock.exe': Loaded 'C:\Windows\SysWOW64\oleacc.dll', Cannot find or open the PDB file
The program '[6560] LostRock.exe: Native' has exited with code 0 (0x0).

And when it isn't commented out...

//...
'LostRock.exe': Loaded 'C:\Windows\SysWOW64\cfgmgr32.dll', Cannot find or open the PDB file
'LostRock.exe': Loaded 'C:\Windows\SysWOW64\devobj.dll', Cannot find or open the PDB file
'LostRock.exe': Loaded 'C:\Windows\SysWOW64\wintrust.dll', Cannot find or open the PDB file
'LostRock.exe': Loaded 'C:\Windows\SysWOW64\crypt32.dll', Cannot find or open the PDB file
'LostRock.exe': Loaded 'C:\Windows\SysWOW64\msasn1.dll', Cannot find or open the PDB file
'LostRock.exe': Unloaded 'C:\Windows\SysWOW64\setupapi.dll'
'LostRock.exe': Unloaded 'C:\Windows\SysWOW64\devobj.dll'
'LostRock.exe': Unloaded 'C:\Windows\SysWOW64\cfgmgr32.dll'
'LostRock.exe': Loaded 'C:\Windows\SysWOW64\clbcatq.dll', Cannot find or open the PDB file
'LostRock.exe': Loaded 'C:\Windows\SysWOW64\oleacc.dll', Cannot find or open the PDB file
The thread 'Win32 Thread' (0xb94) has exited with code 0 (0x0).

The program '[8096] LostRock.exe: Native' has exited with code 0 (0x0). //This is called when I click "Stop Debugging"

P.S. I know it is CreateDXGIFactory because if I comment it out, the program exits correctly.

© Game Development or respective owner

Related posts about directx11

Related posts about dxgi