1

如何使用此代码防止访问冲突错误:

function UsbWndProc(hWnd: hWnd; Msg: UINT; wParam, lParam: Longint)
  : Longint; stdcall;
begin
  try
    if fPrevWndProc <> nil then
      Result := CallWindowProc(fPrevWndProc, hWnd, Msg, wParam, lParam);
    if (Msg = WM_DEVICECHANGE) and (mUSBDetector <> nil) then
      mUSBDetector.DeviceChanged(Msg, wParam, lParam);
  except
    ;
  end;
end;

这是来自 MadExcept 的日志:

   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    > call    -$431be9 ($412ad4)     ; Winapi.Windows.CallWindowProc
008446b8
008446bd      mov     [ebp-4], eax

第 86 行是Result := CallWindowProc(fPrevWndProc, hWnd, Msg, wParam, lParam);

我已添加try-except,但有时会在我的程序中出现访问冲突错误,尤其是在我关闭程序时。

谢谢。

4

0 回答 0