title: DirectX 11 Access Violation
body: <p>I've took a working CSprite class from my old DirectX 11.0 project which i builded with Visual Studio 2010. I just modified it a little bit, I extract some varibles, getter &amp; setter and put it in a new class named CObject. Now i want to build with Visual Studio 2012.</p>
  
  <p>But now I get everytime I try to create a object of CSprite a "Access Violation" error from Visual Studio: </p>
  
  <blockquote>
    <p>Unhandled exception at 0x010A74A8 in Engine.exe: 0xC0000005: Access violation reading location 0x00000000.</p>
  </blockquote>
  
  <p>It seems to be i can't create any DirectX 11 interface object and if I want to read out the object Visual Studio just say's "Unable to read memory".</p>
  
  <p>Sourcecode:</p>
  
  <p>Object.h</p>
  
  <pre><code>#ifndef __OBJECT_H__
  #define __OBJECT_H__
  
  #include "../DX11Core/DX11Util.h"
  
  class CObject
  {
  public:
      CObject();
      ~CObject(){}
  
      void SetStatus( STRING _status )    { m_status = _status; }
      void SetTag( STRING _tag )          { m_tag = _tag; }
      void SetPosition( float _x, float _y, float _z ) { SetPosition( XMFLOAT3( _x, _y, _z ) ); }
      void SetPosition( XMFLOAT3 _pos )   { m_position = _pos; }
      void SetRotation( float _rot )      { m_fRotation = _rot; }
      void SetScale( float _x, float _y ) { SetScale( XMFLOAT3( _x, _y, 1 ) ); }
      void SetScale( XMFLOAT3 _scale )    { m_scale = _scale; }
      void SetAlpha( float _alpha )       { m_fAlpha = Clamp( _alpha, 0.0f, 1.0f ); }
      void SetActive( bool _active )      { m_bActive = _active; }
  
      XMFLOAT3&amp;   GetPosition()           { return m_position; }
      STRING      GetStatus()             { return m_status; }
      STRING      GetTag()                { return m_tag; }
      float       GetXPosition()          { return m_position.x; }
      float       GetYPosition()          { return m_position.y; }
      float       GetZPosition()          { return m_position.z; }
      float       GetRotation()           { return m_fRotation; }
      XMFLOAT3&amp;   GetScale()              { return m_scale; }
      float       GetXScale()             { return m_scale.x; }
      float       GetYScale()             { return m_scale.y; }
      bool        IsActive()              { return m_bActive; }
  
  protected:
      XMFLOAT3    m_position;
      STRING      m_tag;
      STRING      m_status;
      float       m_fRotation;
      XMFLOAT3    m_scale;
      float       m_fAlpha;
      bool        m_bActive;
  };
  
  #endif // __OBJECT_H__
  </code></pre>
  
  <p>Sprite.h</p>
  
  <pre><code>#ifndef __SPRITE_H__
  #define __SPRITE_H__
  
  #include "Object.h"
  #include "../DX11Core/DX11Core.h"
  #include "../DX11Core/GraphicHelper.h"
  
  struct Vertex
  {
      XMFLOAT3 Pos;
      XMFLOAT2 Tex;
  };
  
  class CSprite : public CObject
  {
  public:
      CSprite( STRING _filename );
      ~CSprite()
      {
          RELEASE_COM( m_pVertices );
          RELEASE_COM( m_pTextureSampler );
          RELEASE_COM( m_pTextureSRV );
      }
  
      int Initialize();
      int Update( const float _dt );
      void Render();
  
  #pragma region Getter &amp; Setter
  
      void SetTexture( STRING _filename );
      void SetFrameCount( int _framecount ) { m_nFrameCount = _framecount; }
      void SetFrame( int _frameidx )
      {
          if( _frameidx &gt;= 0 &amp;&amp; _frameidx &lt; m_nFrameCount )
          {
              m_nActualFrame = _frameidx;
          }
          BuildVertices();
      }
      void SetPosition( XMFLOAT3&amp; _pos )
      {
          m_position = _pos;
          m_position.x -= ( theDXCore-&gt;GetClientWidth() / 2 );
          m_position.y *= -1.0f;
          m_position.y += ( theDXCore-&gt;GetClientHeight() / 2 );
      }
  
      float       GetLeftBounding() const { return ( m_position.x + m_fBoundingLeft ); }
      float       GetRightBounding() const    { return ( m_position.x + m_fBoundingRight ); }
      float       GetTopBounding() const  { return ( m_position.y + m_fBoundingTop ); }
      float       GetBottomBounding() const { return ( m_position.y + m_fBoundingBottom ); }
      int         GetFrameIdx() const { return m_nActualFrame; }
      XMMATRIX    GetWorldMatrix() const;
      Vertex      GetVertex( int _i) const { return tempVertex[_i]; }
  
  #pragma endregion
  
  private:
      void BuildVertices();
  
      int         m_nFrameCount;
      int         m_nActualFrame;
      float       m_fFrameDT;
      float       m_fActualFrameDT;
  
      float       m_fBoundingLeft;
      float       m_fBoundingRight;
      float       m_fBoundingTop;
      float       m_fBoundingBottom;
  
      Vertex tempVertex[6];
  
      ID3D11Buffer*                           m_pVertices;
      ID3D11ShaderResourceView*               m_pTextureSRV;
      ID3D11SamplerState*                     m_pTextureSampler;
      ID3DX11EffectMatrixVariable*            m_pFXWorld;
      ID3DX11EffectShaderResourceVariable*    m_pFXTexture;
      ID3DX11EffectSamplerVariable*           m_pFXTextureSampler;
  };
  
  #endif //__SPRITE_H__
  </code></pre>
  
  <p>Sprite.cpp | I have two comments inserted where the problems start</p>
  
  <pre><code>#include "Sprite.h"
  #include "SpriteManager.h"
  
  CSprite::CSprite( STRING _filename )
      : m_nFrameCount( 1 )
      , m_nActualFrame( 0 )
      , m_pTextureSRV( 00 )
      , m_pTextureSampler( 00 )
      , m_pVertices( 00 )
      , m_pFXWorld( 00 )
      , m_pFXTexture( 00 )
      , m_pFXTextureSampler( 00 )
      , m_fFrameDT( 1000.0f )
      , m_fActualFrameDT( 0.0f )
  {
      GraphicHelper::CreateSRV( m_pTextureSRV, _filename ); // can't create ID3D11ShaderResourceView
  
      this-&gt;Initialize();
  }
  
  int CSprite::Initialize()
  {
      D3D11_SAMPLER_DESC samplerDesc = {};
      samplerDesc.AddressU = samplerDesc.AddressV = samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
      samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
      samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
      //  samplerDesc.Filter = D3D11_FILTER_ANISOTROPIC;
      //  samplerDesc.MaxAnisotropy = 4;
      samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
      HR( theDevice-&gt;CreateSamplerState( &amp;samplerDesc, &amp;m_pTextureSampler ) );
  
      BuildVertices( );
  
      D3D11_SAMPLER_DESC colorMapDesc = {};
      colorMapDesc.AddressU = colorMapDesc.AddressV = colorMapDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
      colorMapDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
      colorMapDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
      //  colorMapDesc.Filter = D3D11_FILTER_ANISOTROPIC;
      //  colorMapDesc.MaxAnisotropy = 4;
      colorMapDesc.MaxLOD = D3D11_FLOAT32_MAX;
      HR( theDevice-&gt;CreateSamplerState( &amp;colorMapDesc, &amp;m_pTextureSampler ) );
  
      m_pFXWorld          = theSpriteManager-&gt;GetFX()-&gt;GetVariableByName( "gWorld" )-&gt;AsMatrix();
      m_pFXTexture        = theSpriteManager-&gt;GetFX()-&gt;GetVariableByName( "gTexture" )-&gt;AsShaderResource();
      m_pFXTextureSampler = theSpriteManager-&gt;GetFX()-&gt;GetVariableByName( "gTextureSampler" )-&gt;AsSampler();
  
      return 0;
  }
  
  void CSprite::SetTexture( STRING _filename )
  {
      GraphicHelper::CreateSRV( m_pTextureSRV, _filename );
  }
  
  XMMATRIX CSprite::GetWorldMatrix() const
  {
      XMMATRIX translation = XMMatrixTranslation( m_position.x, m_position.y, m_position.z );
      XMMATRIX rotationZ = XMMatrixRotationZ( m_fRotation );
      XMMATRIX scale = XMMatrixScaling( m_scale.x, m_scale.y, 1.0f );
  
      return scale * rotationZ * translation;
  }
  
  void CSprite::BuildVertices()
  {
      RELEASE_COM( m_pVertices );
  
      ID3D11Resource* pcolorTex = 00;
      m_pTextureSRV-&gt;GetResource( &amp;pcolorTex ); //throw access violation
  
      D3D11_TEXTURE2D_DESC colorTexDesc = {};
      ( reinterpret_cast&lt;ID3D11Texture2D*&gt;( pcolorTex ) )-&gt;GetDesc( &amp;colorTexDesc );
      pcolorTex-&gt;Release();
  
      colorTexDesc.Width /= m_nFrameCount;
      // Calculate corners for uneven boundings
      float left = -(float)(int)( colorTexDesc.Width * 0.5f );
      float right = (float)((int)( colorTexDesc.Width * 0.5f ) );
      if( colorTexDesc.Width % 2 == 1 ) right += 1.0f;
      m_fBoundingLeft = left;
      m_fBoundingRight = right;
  
      float top = (float)(int)( colorTexDesc.Height * 0.5f );
      float bottom = -(float)(int)( colorTexDesc.Height * 0.5f );
      if( colorTexDesc.Height % 2 == 1 ) bottom -= 1.0f;
      m_fBoundingTop = top;
      m_fBoundingBottom = bottom;
  
      float FrameWidth = 1.0f / m_nFrameCount;
      // Create Vertexbuffer
  
      Vertex vertices[] =
      {
          { XMFLOAT3( right, top, 1.0f ),    XMFLOAT2( FrameWidth * ( m_nActualFrame + 1 ), 0.0f ) },
          { XMFLOAT3( right, bottom, 1.0f ), XMFLOAT2( FrameWidth * ( m_nActualFrame + 1 ), 1.0f ) },
          { XMFLOAT3( left, bottom, 1.0f ),  XMFLOAT2( FrameWidth * ( m_nActualFrame     ), 1.0f ) },
  
          { XMFLOAT3( left, bottom, 1.0f ),  XMFLOAT2( FrameWidth * ( m_nActualFrame     ), 1.0f ) },
          { XMFLOAT3( left, top, 1.0f ),     XMFLOAT2( FrameWidth * ( m_nActualFrame     ), 0.0f ) },
          { XMFLOAT3( right, top, 1.0f ),    XMFLOAT2( FrameWidth * ( m_nActualFrame + 1 ), 0.0f ) },
      };
  
      D3D11_BUFFER_DESC vdesc = {};
      vdesc.Usage = D3D11_USAGE_IMMUTABLE;
      vdesc.ByteWidth = sizeof( Vertex ) * ARRAYSIZE( vertices );
      vdesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
      D3D11_SUBRESOURCE_DATA vdata = {};
      vdata.pSysMem = vertices;
      HR( theDevice-&gt;CreateBuffer( &amp;vdesc, &amp;vdata, &amp;m_pVertices ) );
  
      memcpy( &amp;tempVertex, vertices, sizeof( vertices ) );
  }
  
  int CSprite::Update( const float _dt )
  {
      m_fActualFrameDT -= _dt;
      if( m_fActualFrameDT &lt; 0.0f )
      {
          m_fActualFrameDT = m_fFrameDT;
  
          int i = m_nActualFrame + 1;
          if( i &gt;= m_nFrameCount ) i = 0;
  
          SetFrame( i );
      }
  
      return ERROR_SUCCESS;
  }
  
  void CSprite::Render()
  {
      UINT stride = sizeof( Vertex );
      UINT offset = 0;
      theContext-&gt;IASetVertexBuffers( 0, 1, &amp;m_pVertices, &amp;stride, &amp;offset );
      XMMATRIX world = GetWorldMatrix();
      m_pFXWorld-&gt;SetMatrix( reinterpret_cast&lt;float*&gt;( &amp;world ) );
      m_pFXTexture-&gt;SetResource( m_pTextureSRV );
      m_pFXTextureSampler-&gt;SetSampler( 0, m_pTextureSampler );
  
      D3DX11_TECHNIQUE_DESC techDesc = {};
      theSpriteManager-&gt;GetFXTech()-&gt;GetDesc( &amp;techDesc );
  
      for( UINT p = 0; p &lt; techDesc.Passes; ++p )
      {
          theSpriteManager-&gt;GetFXTech()-&gt;GetPassByIndex( p )-&gt;Apply( 0, theContext );
  
          theContext-&gt;Draw( 6, 0 );
      }
  }
  </code></pre>