title: Can someone explain why I still have live objects after releasing the pointers to them?
body: <p>I am experimenting with the singleton pattern to make it easier to use some common interfaces of the DirectX API, but I have a problem I can't seem to solve. </p>
  
  <p>This is my definition:</p>
  
  <pre><code>#include &lt;d3d11.h&gt;
  #include &lt;d3dx11.h&gt;
  #include &lt;DxErr.h&gt;
  
  #pragma comment(lib, "d3d11.lib")
  #pragma comment(lib, "d3dx11.lib")
  #pragma comment(lib, "DxErr.lib")
  
  class Core
  {
  public:
      ~Core();
  
      static ID3D11Device*&amp; getDevice();
      static ID3D11DeviceContext*&amp; getContext();
      static IDXGISwapChain*&amp; getSwapChain();
      static ID3D11RenderTargetView*&amp; getRenderTargetView();
      static ID3D11Debug*&amp; getDebug();
  
      static HRESULT Initialize(HWND hwnd);
      static void Reset();
  
  private:
      Core();
  
      HRESULT CreateDevice();
  
      static Core*            instance;
  
      ID3D11Device*           device;
      ID3D11DeviceContext*    context;
  
      IDXGISwapChain*         swapChain;
      IDXGIDevice1*           ddevice;
      IDXGIAdapter1*          adapter;
      IDXGIFactory1*          factory;
      ID3D11RenderTargetView* rtv;
      ID3D11Debug*            debug;
  
      Core(Core const&amp;);             
      void operator=(Core const&amp;);
  };
  </code></pre>
  
  <p>This is the implementation:</p>
  
  <pre><code>#include "Core.h"
  
  Core* Core::instance;
  
  Core::Core()
  {
      HRESULT hr;
      hr = CreateDevice();
      if(FAILED(hr)) throw hr;
  }
  
  Core::~Core()
  {
      if (device) device-&gt;Release();
      if (context) context-&gt;Release();
  
      if (swapChain) swapChain-&gt;Release();
      if (ddevice) ddevice-&gt;Release();
      if (adapter) adapter-&gt;Release();
      if (factory) factory-&gt;Release();
      if (rtv) rtv-&gt;Release();
  
      debug-&gt;ReportLiveDeviceObjects(D3D11_RLDO_DETAIL);
  
  }
  
  //--------------------------------------------------------------------------------
  // Get Functions
  //--------------------------------------------------------------------------------
  
  ID3D11Device*&amp; Core::getDevice()
  {
      if (!instance)
      {
          try
          {
              instance = new Core();
          }
          catch (HRESULT hr)
          {
              throw hr;
          }
      }
      return instance-&gt;device;
  }
  
  ID3D11DeviceContext*&amp; Core::getContext()
  {
      if (!instance)
      {
          try
          {
              instance = new Core();
          }
          catch (HRESULT hr)
          {
              throw hr;
          }
      }
      return instance-&gt;context;
  }
  
  
  IDXGISwapChain*&amp; Core::getSwapChain()
  {
      if (!instance)
      {
          try
          {
              instance = new Core();
          }
          catch (HRESULT hr)
          {
              throw hr;
          }
      }
  
      return instance-&gt;swapChain;
  }
  
  ID3D11RenderTargetView*&amp; Core::getRenderTargetView()
  {
      if (!instance)
      {
          try
          {
              instance = new Core();
          }
          catch (HRESULT hr)
          {
              throw hr;
          }
      }
      return instance-&gt;rtv;
  }
  
  ID3D11Debug*&amp; Core::getDebug()
  {
      if (!instance)
      {
          try
          {
              instance = new Core();
          }
          catch (HRESULT hr)
          {
              throw hr;
          }
      }
      return instance-&gt;debug;
  }
  
  
  //--------------------------------------------------------------------------------
  // Other Functions
  //--------------------------------------------------------------------------------
  
  HRESULT Core::CreateDevice()
  {
      D3D_FEATURE_LEVEL featureLevels[] =
      {
          D3D_FEATURE_LEVEL_11_0,
          D3D_FEATURE_LEVEL_10_1,
          D3D_FEATURE_LEVEL_10_0,
      };
      D3D_FEATURE_LEVEL featureLevel;
  
      UINT flags = NULL;
  #ifdef _DEBUG
      flags |= D3D11_CREATE_DEVICE_DEBUG;
  #endif
  
      HRESULT hr;
  
      hr = D3D11CreateDevice(
          NULL,
          D3D_DRIVER_TYPE_HARDWARE,
          NULL,
          flags,
          featureLevels,
          ARRAYSIZE(featureLevels),
          D3D11_SDK_VERSION,
          &amp;device,
          &amp;featureLevel,
          &amp;context);
      if(FAILED(hr)) return hr;
  
      hr = device-&gt;QueryInterface(__uuidof(ID3D11Debug), (void**) &amp;debug);
      if(FAILED(hr)) return hr;
      hr = device-&gt;QueryInterface(__uuidof(IDXGIDevice1), (void**) &amp;ddevice);
      if(FAILED(hr)) return hr;
      hr = ddevice-&gt;GetParent(__uuidof(IDXGIAdapter1), (void**) &amp;adapter);
      if(FAILED(hr)) return hr;
      hr = adapter-&gt;GetParent(__uuidof(IDXGIFactory1), (void**) &amp;factory);
      if(FAILED(hr)) return hr;
  
      return S_OK;
  }
  
  HRESULT Core::Initialize(HWND hwnd)
  {
      // Create the swap chain
      RECT rc;
      GetClientRect(hwnd, &amp;rc);
      UINT width = rc.right - rc.left;
      UINT height = rc.bottom - rc.top;
  
      DXGI_SWAP_CHAIN_DESC sd;
      ZeroMemory( &amp;sd, sizeof( sd ) );
      sd.BufferCount = 1;
      sd.BufferDesc.Width = width;
      sd.BufferDesc.Height = height;
      sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
      sd.BufferDesc.RefreshRate.Numerator = 60;
      sd.BufferDesc.RefreshRate.Denominator = 1;
      sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
      sd.OutputWindow = hwnd;
      sd.SampleDesc.Count = 1;
      sd.SampleDesc.Quality = 0;
      sd.Windowed = TRUE;
  
      if (!instance)
      {
          try
          {
              instance = new Core();
          }
          catch (HRESULT hr)
          {
              return hr;
          }
      }
  
      instance-&gt;factory-&gt;CreateSwapChain(instance-&gt;device, &amp;sd, &amp;instance-&gt;swapChain);
  
      // Create and set the render target view
      HRESULT hr;
      ID3D11Texture2D* renderingBuffer;   
      hr = instance-&gt;swapChain-&gt;GetBuffer(0, __uuidof(ID3D11Texture2D), (void**) &amp;renderingBuffer);
      if(FAILED(hr)) 
      {
          renderingBuffer-&gt;Release();
          return hr;
      }
      hr = instance-&gt;device-&gt;CreateRenderTargetView(renderingBuffer, NULL, &amp;instance-&gt;rtv);
      renderingBuffer-&gt;Release();
      if(FAILED(hr)) return hr;
  
      // TODO: create and set the depth stencil view
  
      instance-&gt;context-&gt;OMSetRenderTargets(1, &amp;instance-&gt;rtv, nullptr);
  
      return S_OK;
  }
  
  void Core::Reset()
  {
      delete instance;
  }
  </code></pre>
  
  <p>The destructor getting called, but still I see this in my output window once I exit the application:</p>
  
  <pre><code>D3D11 WARNING: Live ID3D11Device at 0x007A201C, Refcount: 2 [ STATE_CREATION WARNING #441: LIVE_DEVICE]
  D3D11 WARNING:  Live ID3D11Context at 0x007A3620, Refcount: 0, IntRef: 1 [ STATE_CREATION WARNING #2097226: LIVE_CONTEXT]
  D3D11 WARNING:  Live ID3DDeviceContextState at 0x007ADBB0, Refcount: 0, IntRef: 1 [ STATE_CREATION WARNING #3145742: LIVE_DEVICECONTEXTSTATE]
  D3D11 WARNING:  Live ID3D11BlendState at 0x007B3D84, Refcount: 0, IntRef: 1 [ STATE_CREATION WARNING #435: LIVE_BLENDSTATE]
  D3D11 WARNING:  Live ID3D11DepthStencilState at 0x007B3ECC, Refcount: 0, IntRef: 1 [ STATE_CREATION WARNING #436: LIVE_DEPTHSTENCILSTATE]
  D3D11 WARNING:  Live ID3D11RasterizerState at 0x007B403C, Refcount: 0, IntRef: 1 [ STATE_CREATION WARNING #437: LIVE_RASTERIZERSTATE]
  D3D11 WARNING:  Live ID3D11Sampler at 0x007B428C, Refcount: 0, IntRef: 1 [ STATE_CREATION WARNING #434: LIVE_SAMPLER]
  D3D11 WARNING:  Live ID3D11Query at 0x007B440C, Refcount: 0, IntRef: 1 [ STATE_CREATION WARNING #438: LIVE_QUERY]
  D3D11 WARNING:  Live IDXGISwapChain at 0x007B45B8, Refcount: 0 [ STATE_CREATION WARNING #442: LIVE_SWAPCHAIN]
  D3D11 WARNING:  Live ID3D11Texture2D at 0x007B4B1C, Refcount: 0, IntRef: 1 [ STATE_CREATION WARNING #425: LIVE_TEXTURE2D]
  D3D11 WARNING:  Live ID3D11RenderTargetView at 0x007B4EBC, Refcount: 0, IntRef: 0 [ STATE_CREATION WARNING #428: LIVE_RENDERTARGETVIEW]
  D3D11 WARNING:  Live ID3D11Texture2D at 0x007B55EC, Refcount: 0, IntRef: 1 [ STATE_CREATION WARNING #425: LIVE_TEXTURE2D]
  </code></pre>
  
  <p>I also checked whether the Release() functions in the destructor are actually called and they are. </p>
  
  <p>I must be missing something very obvious and would greatly appreciate it if someone can point it out to me.</p>
  
  <p>Also, is this my using of the singleton pattern in this case a smart thing to do?</p>
  
  <p>Thanks.</p>
  
  <p>EDIT:</p>
  
  <p>Here is my DirectX class, where I use the Core class:</p>
  
  <pre><code>#include "DirectX.h"
  
  DirectX::DirectX() {}
  DirectX::DirectX(HWND hwnd)
  {
      HRESULT hr;
      hr = Init(hwnd);
      if(FAILED(hr)) throw hr;
      hr = Load();
      if(FAILED(hr)) throw hr;
  }
  DirectX::~DirectX()
  {
      Core::Reset();
  }
  
  HRESULT DirectX::Init(HWND hwnd)
  {
      HRESULT hr;
      hr = Core::Initialize(hwnd);
      if(FAILED(hr)) return hr;
  
      return S_OK;
  }
  
  HRESULT DirectX::Load()
  {
      return S_OK;
  }
  
  HRESULT DirectX::Update()
  {
      return S_OK;
  }
  
  HRESULT DirectX::Render()
  {
      try 
      {
          float backgroundcolor[4] = {1.0f, 1.0f, 0.0f, 1.0f};
          Core::getContext()-&gt;ClearRenderTargetView(Core::getRenderTargetView(), backgroundcolor);
          Core::getSwapChain()-&gt;Present(0, 0);
      }
      catch (HRESULT hr)
      {
          return hr;
      }
      return S_OK;
  }
  </code></pre>