title: Error "Access violation reading location 0x00000008" with msftedit.dll RichEdit control inside SetWindowSubclass
body: <p>I have edit control in separate subclass file. After getting to work SetWindowSubclass function (I am new to C++ and previously I used SetWindowLongPtr for subclassing where it worked fine but I was given advice to start using SetWindowSubclass), I got this issue:</p>
  
  <p>After compiling the program, application draws the empty window, which immediately stops responding (and I have to close it via Task Manager).
  The result error in Output window:</p>
  
  <blockquote>
    <p>Exception thrown at 0x635F3DEF (msftedit.dll) in TaskTracklist.exe:
    0xC0000005: Access violation reading location 0x00000008.</p>
  </blockquote>
  
  <p>Whole code:</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;windows.h&gt;
  #include &lt;commctrl.h&gt;
  #include &lt;richedit.h&gt;
  #include &lt;string.h&gt;
  #include "SearchEditBox.h"
  
  /*global vars*/
  LPCWSTR SearchEditBox::editBoxDefText = L"Search...";
  bool SearchEditBox::firstLoad = false;
  int SearchEditBox::width = 0;
  int SearchEditBox::height = 0;
  HWND SearchEditBox::editBox;
  
  /*functions*/
  LRESULT CALLBACK EditBoxProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uint_ptr, DWORD dwRefData);
  void ChangeEdit(const HWND hwnd, const bool change);
  
  /*set-get functions*/
  HWND SearchEditBox::getEditBox()
  {
      return SearchEditBox::editBox;
  }
  
  SearchEditBox * SearchEditBox::CreateEditBox(HINSTANCE hInst, HWND hwnd, int pos_x, int pos_y, int width, int height) {
      SearchEditBox * p_SearchEditBox = new SearchEditBox;
  
      LoadLibrary(TEXT("Msftedit.dll")); //enables RichEdit field
      SearchEditBox::editBox = CreateWindowEx(0, (L"RICHEDIT50W"), editBoxDefText, WS_VISIBLE | WS_CHILD, pos_x + 6, pos_y + 4, width, height, hwnd, NULL, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), p_SearchEditBox);
      SearchEditBox::width = width;
      SearchEditBox::height = height;
  
      if (SearchEditBox::editBox == NULL)
      {
          delete p_SearchEditBox;
          MessageBox(NULL, L"Problem creating the Search box.", L"Error", 0);
          return 0;
      }
  
      SetWindowSubclass(SearchEditBox::editBox, SearchEditBox::EditBoxProc, 0, 0);
  
      return p_SearchEditBox;
  }
  
  LRESULT CALLBACK SearchEditBox::EditBoxProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD dwRefData)
  {
      PAINTSTRUCT ps;
      HDC hdc;
      switch (uMsg)
      {
          case WM_SETFOCUS:
          {
              LPWSTR getText = new TCHAR[10];
              GetWindowText(hwnd, getText, 10);
              if (_tcscmp(getText, editBoxDefText) == 0)
              {
                  SearchEditBox::ChangeEdit(hwnd, 1);
              }
              delete getText;
              break;
          }
          case WM_KILLFOCUS:
          {
              LPWSTR getText = new TCHAR[10];
              if (GetWindowText(hwnd, getText, 10) == 0)
              {
                  SearchEditBox::ChangeEdit(hwnd, 0);
              }
              ::SetFocus(NULL);
              delete getText;
              break;
          }
          case WM_LBUTTONDBLCLK:
              SearchEditBox::ChangeEdit(hwnd, 1);
              break;
          case WM_PAINT:
          {
              hdc = BeginPaint(hwnd, &amp;ps);
  
              RECT rc;
              InvalidateRect(hwnd, &amp;rc, 0);
              if (SearchEditBox::firstLoad == false)
              {
                  SearchEditBox::ChangeEdit(hwnd, 0);
                  ::SetFocus(NULL);
                  SearchEditBox::firstLoad = true;
              }
              HBRUSH hBrush = CreateSolidBrush(RGB(33, 33, 33));
              SelectObject(hdc, hBrush);
              RoundRect(hdc, -6, -4, SearchEditBox::width + 7, SearchEditBox::height + 4, 5, 5);
              EndPaint(hwnd, &amp;ps);
              break;
          }
      }
      return DefSubclassProc(hwnd, uMsg, lParam, wParam);
  }
  </code></pre>
  
  <p>It is clear to me that the problem is with RichEdit control on line with SetWindowSubclass function.</p>
  
  <p>Even though I did not find any topic related specifically to this error in connection to Msftedit.dll, I understood from these articles (<a href="https://stackoverflow.com/questions/6811882/0xc0000005-access-violation-reading-location-0x00000008">here</a> and <a href="https://stackoverflow.com/questions/17665147/how-can-i-fix-this-error-access-violation-reading-location-0x00000008">here</a> and <a href="https://stackoverflow.com/questions/24028420/0xc0000005-access-violation-reading-location-0x00000008-c-code">here</a>) that I may be referencing to NULL pointer, but that does not make sense to me because then I would get error from this bit just above SetWindowSubclass function (which I don't):</p>
  
  <pre><code>    if (SearchEditBox::editBox == NULL)
      {
          delete p_SearchEditBox;
          MessageBox(NULL, L"Problem creating the Search box.", L"Error", 0);
          return 0;
      }
  </code></pre>
  
  <p>I also thought the problem may be with external function (meaning function defined in comctl32 and not inside class) accessing class specific variable (the pointer would <strong>not</strong> be NULL, but it just may not be accessible to the function somehow) so I tried creating local variable just for the SetWindowSubclass function (which did not work either):</p>
  
  <pre><code>HWND newHWND = SearchEditBox::editBox;
  SetWindowSubclass(newHWND, SearchEditBox::EditBoxProc, 0, 0);
  </code></pre>
  
  <p>Using function getEditBox() did not solve that problem either.</p>
  
  <p>I also tried using MSFTEDIT_CLASS instead of (L"RICHEDIT50W") in control declaration (thinking the problem may be inside the control itself) but that did not help either, and trying different versions (for example RICHEDIT51W or RICHEDIT80W) resulted in errors (so I must be working with the correct RichEdit version for VS2015).</p>
  
  <p>I also tried declaring HWND editBox inside CreateEditBox (making it local variable), but that did not help either.</p>
  
  <p>I use free Community version of VS.</p>
  
  <p>EDIT: cleared some remains of my unsuccessful tries from the code.</p>