title: Prevent CallWindowProc causing access violation error
body: <p>How do I prevent Access Violation error with this code:</p>
  
  <pre><code>function UsbWndProc(hWnd: hWnd; Msg: UINT; wParam, lParam: Longint)
    : Longint; stdcall;
  begin
    try
      if fPrevWndProc &lt;&gt; nil then
        Result := CallWindowProc(fPrevWndProc, hWnd, Msg, wParam, lParam);
      if (Msg = WM_DEVICECHANGE) and (mUSBDetector &lt;&gt; nil) then
        mUSBDetector.DeviceChanged(Msg, wParam, lParam);
    except
      ;
    end;
  end;
  </code></pre>
  
  <p>Here's the log from MadExcept:</p>
  
  <pre><code>   callstack crc     : $0d3f54dd, $7c7e133d, $d174bae3
      exception number  : 1
      exception class   : EAccessViolation
      exception message : Access violation at address 0059D5CF in module 'XXX.exe'. Read of address 00000010.
  main thread ($8b0):
  0059d5cf +00f XXX.exe    Vcl.Forms               TCustomForm.IsFormSizeStored
  0059d5b5 +005 XXX.exe    Vcl.Forms               TCustomForm.IsClientSizeStored
  0059fd4b +59b XXX.exe    Vcl.Forms               TCustomForm.CreateParams
  0050073e +02e XXX.exe    Vcl.Controls            TWinControl.CreateWnd
  0059bf99 +005 XXX.exe    Vcl.Forms               TScrollingWinControl.CreateWnd
  0059fdc6 +00a XXX.exe    Vcl.Forms               TCustomForm.CreateWnd
  00500c82 +016 XXX.exe    Vcl.Controls            TWinControl.CreateHandle
  00504a04 +01c XXX.exe    Vcl.Controls            TWinControl.HandleNeeded
  00504a11 +005 XXX.exe    Vcl.Controls            TWinControl.GetHandle
  005a5ae9 +02d XXX.exe    Vcl.Forms               GetTopMostWindows
  765f376c +011 USER32.dll                           EnumWindows
  005a5bad +02d XXX.exe    Vcl.Forms               TApplication.DoNormalizeTopMosts
  005a5c82 +002 XXX.exe    Vcl.Forms               TApplication.NormalizeTopMosts
  005a64c2 +326 XXX.exe    Vcl.Forms               TApplication.WndProc
  0048668c +014 XXX.exe    System.Classes          StdWndProc
  765f1b52 +016 USER32.dll                           CallWindowProcW
  008446b8 +034 XXX.exe    U_Usb     86  +3 UsbWndProc
  77c96fcb +02b ntdll.dll                            KiUserCallbackDispatcher
  004fefae +0ae XXX.exe    Vcl.Controls            TWinControl.Destroy
  
  008446ab 86   push    edi
  008446ac      push    esi
  008446ad      push    ebx
  008446ae      mov     eax, [ebp+8]
  008446b1      push    eax
  008446b2      mov     eax, [$902f6c]
  008446b7      push    eax
  008446b8    &gt; call    -$431be9 ($412ad4)     ; Winapi.Windows.CallWindowProc
  008446b8
  008446bd      mov     [ebp-4], eax
  </code></pre>
  
  <p>Line 86 is <code>Result := CallWindowProc(fPrevWndProc, hWnd, Msg, wParam, lParam);</code></p>
  
  <p>I have added <code>try-except</code> but the Access Violation error sometimes occurs in my program especially when I close the program.</p>
  
  <p>Thanks.</p>