当前位置:  开发笔记 > 开发工具 > 正文

Delphi TThread.CurrentThread和EAccessViolation - 这是一个错误还是我的无能......?

如何解决《DelphiTThread.CurrentThread和EAccessViolation-这是一个错误还是我的无能?》经验,为你挑选了3个好方法。

在Delphi 2009中,我发现无论何时在应用程序中使用TThread.CurrentThread,我都会在应用程序关闭时收到如下错误消息:

Exception EAccessViolation in module ntdll.dll at 0003DBBA.
Access violation at address 7799DBBA in module 'ntdll.dll'.  Write of
address 00000014.

除非它只是我的机器,你可以在几秒钟内复制它:创建一个新的Delphi Forms Application,在表单中添加一个按钮,并使用类似下面的按钮的事件处理程序:

procedure TForm1.Button1Click(Sender: TObject);
begin
  TThread.CurrentThread;
end;

在我的Vista的机器和我的XP的机器都我发现,如果我按一下按钮一切都很好,但如果我做的点击按钮,我得到上面的错误消息,当我关闭应用程序.

所以...我想知道这是不是一个错误,但与此同时我认为我很可能根本不理解你应该如何在Delphi中使用TThreads.我有点像德尔福新手,我很害怕.

使用TThread.CurrentThread有什么明显错误吗?

如果没有,并且你有Delphi 2009,如果你实现我的简单示例项目,你会遇到同样的问题吗?


更新:正如François在下面提到的,这实际上是Delphi 2009中的一个错误 - 你可以在这里投票.


更新:此错误已在Delphi 2010中修复.

François.. 15

不幸的是,它似乎是一个与Classes单元中finalization部分的调用顺序相关联的bug:

DoneThreadSynchronization清除ThreadLock结构,然后
FreeExternalThreads想要销毁刚刚调用时创建的Thread对象CurrentThread,
这需要在调用
EnterCriticalSection(ThreadLock)中初始化ThreadLock TThread.RemoveQueuedEvents...

更新:
现在有一个解决办法补丁QC报告.



1> François..:

不幸的是,它似乎是一个与Classes单元中finalization部分的调用顺序相关联的bug:

DoneThreadSynchronization清除ThreadLock结构,然后
FreeExternalThreads想要销毁刚刚调用时创建的Thread对象CurrentThread,
这需要在调用
EnterCriticalSection(ThreadLock)中初始化ThreadLock TThread.RemoveQueuedEvents...

更新:
现在有一个解决办法补丁QC报告.



2> gabr..:

在CodeGear发布修复程序之前,您可以使用下面的修补程序.将其保存到独立单元中,并在程序中的任何位置使用它.我也会尝试将它添加到QC中.

此版本适用于D2009(原始版),更新1和更新2.

{ Fix Delphi 2009's invalid finalization order in Classes.pas.
  Written by Primoz Gabrijelcic, http://gp.17slon.com.
  No rights reserved - released to public domain.
}
unit FixD2009Classes;

interface

implementation

uses
  Windows,
  SysUtils,
  Classes;

type
  TCode = array [0..109] of byte;

{$WARN SYMBOL_PLATFORM OFF}

procedure PatchClasses;
{$IFDEF ConditionalExpressions}
{$IF RTLVersion = 20}
var
  i         : integer;
  oldProtect: cardinal;
  pCode     : ^TCode;
  tmp       : DWORD;
const
  COffsets_Call: array [1..12] of integer = (0, 15, 24, 34, 49, 59, 69, 79, 89, 94, 99, 109);
  COffset_UnRegisterModuleClasses = 106;
  COffset_DoneThreadSynchronization = 94;
  COffset_FreeExternalThreads = 99;
  CCallDelta = COffset_FreeExternalThreads - COffset_DoneThreadSynchronization;
{$IFEND}
{$ENDIF}
begin
{$IFDEF ConditionalExpressions}
{$IF RTLVersion = 20}
  pCode := pointer(cardinal(@TStreamReader.ReadToEnd) + COffset_UnRegisterModuleClasses);
  Win32Check(VirtualProtect(pCode, COffsets_Call[High(COffsets_Call)], PAGE_READWRITE, oldProtect));
  try
    for i := Low(COffsets_Call) to High(COffsets_Call) do
      if pCode^[COffsets_Call[i]] <> $E8 then
        raise Exception.Create('Unexpected version of Classes - cannot patch');
    tmp := PDword(@pCode^[COffset_DoneThreadSynchronization+1])^;
    PDword(@pCode^[COffset_DoneThreadSynchronization+1])^ :=
      PDword(@pCode^[COffset_FreeExternalThreads+1])^ + CCallDelta;
    PDword(@pCode^[COffset_FreeExternalThreads+1])^ := tmp - CCallDelta;
  finally VirtualProtect(pCode, COffsets_Call[High(COffsets_Call)], oldProtect, oldProtect); end;
{$IFEND}
{$ENDIF}
end;

initialization
  PatchClasses;
end.



3> gabr..:

Delphi 2009 Update 3的补丁单元.

{ Fix Delphi 2009's invalid finalization order in Classes.pas.
  Written by Primoz Gabrijelcic, http://gp.17slon.com.
  No rights reserved - released to public domain.

  D2009 update 3 only.
}
unit FixD2009Classes;

interface

implementation

uses
  Windows,
  SysUtils,
  Classes;

type
  TCode = array [0..144] of byte;

{$WARN SYMBOL_PLATFORM OFF}

procedure PatchClasses;
{$IFDEF ConditionalExpressions}
{$IF RTLVersion = 20}
var
  i         : integer;
  oldProtect: cardinal;
  pCode     : ^TCode;
  tmp       : DWORD;
const
  COffsets_Call: array [1..12] of integer = (0, 15, 24, 42, 47, 58, 73, 91, 101, 111, 134, 139);
  COffset_UnRegisterModuleClasses = 107;
  COffset_DoneThreadSynchronization = 134;
  COffset_FreeExternalThreads = 139;
  CCallDelta = COffset_FreeExternalThreads - COffset_DoneThreadSynchronization;
{$IFEND}
{$ENDIF}
begin
{$IFDEF ConditionalExpressions}
{$IF RTLVersion = 20}
  pCode := pointer(cardinal(@TStreamReader.ReadToEnd) + COffset_UnRegisterModuleClasses);
  Win32Check(VirtualProtect(pCode, COffsets_Call[High(COffsets_Call)], PAGE_READWRITE, oldProtect));
  try
    for i := Low(COffsets_Call) to High(COffsets_Call) do
      if pCode^[COffsets_Call[i]] <> $E8 then
        raise Exception.Create('Unexpected version of Classes - cannot patch');
    tmp := PDword(@pCode^[COffset_DoneThreadSynchronization+1])^;
    PDword(@pCode^[COffset_DoneThreadSynchronization+1])^ :=
      PDword(@pCode^[COffset_FreeExternalThreads+1])^ + CCallDelta;
    PDword(@pCode^[COffset_FreeExternalThreads+1])^ := tmp - CCallDelta;
  finally VirtualProtect(pCode, COffsets_Call[High(COffsets_Call)], oldProtect, oldProtect); end;
{$IFEND}
{$ENDIF}
end;

initialization
  PatchClasses;
end.

推荐阅读
手机用户2402851335
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有