title: ACCESS_VIOLATION exception (0xC0000005) when using IDirect3DTexture9::LockRect
body: <p>Using DirectX 9, I am trying to create and then fill in an <code>LPDIRECT3DTEXTURE9</code> texture in the following way.</p>
  
  <ol>
  <li><p>First, I create the texture with <code>IDirect3DTexture9::CreateTexture</code>:</p>
  
  <pre><code>LPDIRECT3DTEXTURE9 pTexture;
  
  if ( FAILED( pd3dDevice-&gt;CreateTexture( MAX_IMAGE_WIDTH,
                             MAX_IMAGE_HEIGHT,
                             1,
                             0,                 // D3DUSAGE_DYNAMIC,
                             D3DFMT_A8R8G8B8,
                             D3DPOOL_MANAGED,   // D3DPOOL_DEFAULT,
                             &amp;pTexture,
                             NULL ) ) )
  {
      // Handle error case
  }
  </code></pre></li>
  <li><p>Then, I try and lock a rectangle on the texture as follows:</p>
  
  <pre><code>unsigned int uiSize = GetTextureSize();
  D3DLOCKED_RECT rect;
  ARGB BlackColor = { (char)0xFF, (char)0xFF, (char)0xFF, (char)0x00 };
  ::ZeroMemory( &amp;rect, sizeof( D3DLOCKED_RECT ) );
  
  // Lock outline texture to rect, and then cast rect to bits and use bits as outlineTexture access point
  
  if ( pTexture == NULL )
  {
      return ERROR_NOT_INITIALIZED;
  }
  
  pTexture-&gt;LockRect( 0, &amp;rect, NULL, D3DLOCK_NOSYSLOCK );          // Consider ?
  ARGB* bits = (ARGB*)rect.pBits;
  
  for ( unsigned int uiPixel = 0; uiPixel &lt; uiSize; ++uiPixel )
  {
      // Copy all black pixels only
      if ( compositeMask[uiPixel] == BlackColor )
      {
          bits[uiPixel] = compositeMask[uiPixel];
      }
  }
  
  pTexture-&gt;UnlockRect( 0 );
  
  return ERROR_SUCCESS;
  </code></pre></li>
  </ol>
  
  <p><code>ARGB</code> is just a struct defined as follows:</p>
  
  <pre><code>struct ARGB
  {
      char b;
      char g;
      char r;
      char a;
  
      bool operator==( ARGB&amp; comp )
      {
          if ( a == comp.a &amp;&amp;
               r == comp.r &amp;&amp;
               g == comp.g &amp;&amp;
               b == comp.b )
              return TRUE;
          else
              return FALSE;
      }
  
      bool operator!=( ARGB&amp; comp )
      {
          return !( this-&gt;operator==( comp ) );
      }
  };
  </code></pre>
  
  <p>What I want to do is pre-calculate an array of pixel data (a black outline) depending on an in-application algorithm, and then only write the pure black pixels from that set of pixel data onto my <code>LPDIRECT3DTEXTURE9</code> to be rendered later.</p>
  
  <p>The application currently throws a ACCESS_VIOLATION exception (0xC0000005) at the LockRect call. Can anyone possibly explain why?</p>
  
  <p>Here's the exact exception detail:</p>
  
  <p>Unhandled exception at 0x0132F261 in TestApp.exe: 0xC0000005: Access violation reading location 0x00000001.</p>
  
  <p>The location varied between <code>0x00000000</code> and <code>0x00000001</code>... Does that hint at anything?</p>
  
  <p>Also, if there's a better way to do what I am trying to do, then I'd be all ears :)</p>