title: Exception thrown: read access violation. this->m_pRenderTarget was nullptr. c++
body: <p>I'm not sure where the error is. I was watching a video series on youtube to learn how to write game engines and I will admit I'm not the best coder (I actually started this project as a learning experience) and that's probably why I can't find this error, but I have rewatched the graphics part at least 10 times and still haven't had any luck finding the problem. Can someone help find this and explain to me where and why this is happening? </p>
  
  <pre><code>graphics.h
  #ifndef _Graphics_H
  #define _Graphics_H
  //C run time header files 
  
  //ADDITIONAL INCLUDES 
  #include "System.h"
  
  //#ifndef _Logger_H
  //  #include "Logger.h"
  //#endif
  
  #ifndef _2DUTILL_H
      #include "d2dutill.h"
  #endif
  
  //forward declrations 
  class Window;
  
  
  //struct 
  struct GraphicsData :public SystemData
  {
  public: 
      GraphicsData(Window* wnd = nullptr);
      Window* pWnd;
  };
  class Graphics : public System
  {
      friend class Engine;
  public:
      HRESULT OnResize(UINT width, UINT height);
      ID2D1HwndRenderTarget* GetRenderTarget()            { return m_pRenderTarget; }
      IWICImagingFactory* GetImageFactory()               { return m_pImageFactory; }
      ID2D1SolidColorBrush* GetColorBrush()               { return m_pColorBrush; }
      ID2D1Factory* GetD2DFactory()                       { return m_pD2DFactory; }
  
  protected:
      Graphics(const GraphicsData&amp; data);
      virtual ~Graphics();
      Graphics(const Graphics&amp; other);
      Graphics&amp; operator = (const Graphics&amp; tref);
      bool initialize();
      bool ShutDown();
  private: 
  //  void* operator new(size_t size);
  //  void operator delete(void* pdelete);
  
      HRESULT CreateDeviceIndependentResorces();
      HRESULT CreateDeviceDependentResorces();
  
      void BeginDraw();
      HRESULT EndDraw();
      void DiscardDeviceResorces();
      //members
      ID2D1HwndRenderTarget* m_pRenderTarget;
      IWICImagingFactory* m_pImageFactory;
      ID2D1SolidColorBrush* m_pColorBrush;
      ID2D1Factory* m_pD2DFactory;
  
      Window* m_pWindow;
  };
  #endif 
  </code></pre>
  
  <p>Some things are not implemented yet due to the way I like to do testing.</p>
  
  <p>Graphics.cpp</p>
  
  <pre><code>include "Graphics.h"
  #include "Window.h"
  #ifndef _DeleteMacros_H
      #include "DeleteMacros.h"
  #endif
  
  GraphicsData::GraphicsData(Window* wnd)
      :
      SystemData(SystemType::Sys_Graphics),
      pWnd(wnd)
      {}
  
  
  
  Graphics::Graphics(const GraphicsData&amp; data)
      :
      System(data),
      m_pRenderTarget(nullptr),
      m_pD2DFactory(nullptr),
      m_pColorBrush(nullptr),
      m_pImageFactory(nullptr),
      m_pWindow(data.pWnd)
  {}
  
  //void Graphics::operator new(size_T size)
  //{
  //  return MEMORYMANAGER-&gt;alloc(size);
  //}
  //void Graphics::operator delete(void* pDelete)
  //{
  //  MEMORYMANAGER-&gt;free(pDelete);
  //}
  
  
  
  Graphics::~Graphics()
  {}
  
  HRESULT Graphics::OnResize(UINT width, UINT height)
  {
      //this method can fail and is safe to ignore the warning 
      //error will be returned until the end draw method is called again 
  
      if (m_pRenderTarget)
      return m_pRenderTarget-&gt;Resize(D2D1::SizeU(width, height));
      else return S_FALSE;
  }
  
  bool Graphics::initialize()
  {
      System::Initialize();
      HRESULT hr = CreateDeviceIndependentResorces();
      if (FAILED(hr))
          return false;
      hr = CreateDeviceDependentResorces();
      if (FAILED(hr))
          return false;
  
  //  Logger::log(_T("initializing Graphics Complete"),LOGTYPE_INFO, false);
  
      return true;
  }
  
  bool Graphics::ShutDown()
  {
      System::ShutDown();
  
      SafeRelease(m_pD2DFactory);
      SafeRelease(m_pColorBrush);
      SafeRelease(m_pImageFactory);
      SafeRelease(m_pRenderTarget);
      return true;
  
  }
  
  HRESULT Graphics::CreateDeviceIndependentResorces()
  {
      HRESULT hr = S_OK;
  // create Direct2D factory
      hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &amp;m_pD2DFactory);
      hr = CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (LPVOID*)&amp;m_pImageFactory);
  
      return hr;
  }
  
  HRESULT Graphics::CreateDeviceDependentResorces()
  {
      HRESULT hr = S_OK;
      if (m_pRenderTarget==nullptr)
      {
          //get window handel
          HWND hWnd = m_pWindow-&gt;GetWindowHandle();
  
          RECT rc;
          GetClientRect(hWnd, &amp;rc);
          D2D1_SIZE_U size = D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top);
  
          // create direct2d render target 
          hr = m_pD2DFactory-&gt;CreateHwndRenderTarget(D2D1::RenderTargetProperties(), D2D1::HwndRenderTargetProperties(hWnd, size), &amp;m_pRenderTarget);
          if (FAILED(hr))
              return hr;
  
          //create a brush
          hr = m_pRenderTarget-&gt;CreateSolidColorBrush((D2D1::ColorF) D2D1::ColorF::Black, &amp;m_pColorBrush);
          if (FAILED(hr))
              return hr;
      }
      return hr;
  }
  
  
  void Graphics::DiscardDeviceResorces()
  {
      SafeRelease(m_pRenderTarget);
  }
  
  void Graphics::BeginDraw()
  {
  
          m_pRenderTarget-&gt;BeginDraw();
          m_pRenderTarget-&gt;Clear(D2D1::ColorF(D2D1::ColorF::White));
  }
  
  HRESULT Graphics::EndDraw()
  {
      return m_pRenderTarget-&gt;EndDraw();
  
  }
  </code></pre>
  
  <p>The exception is thrown at <code>m_pRenderTarget-&gt;BeginDraw();</code></p>