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

时间:2008-10-09 17:20:59

标签: delphi delphi-2009 tthread

在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机器上,我发现,如果我点击按钮一切都很好,但如果我点击按钮我关闭应用程序时收到上述错误消息。

所以......我想知道这是不是一个bug,但与此同时我认为我很可能根本不理解你应该如何使用Delphi中的TThreads。我恐怕是一个特尔斐新手。

使用TThread.CurrentThread是否有明显错误?

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


更新:正如François在下面提到的,这实际上是Delphi 2009中的一个错误 - 你可以vote for it here


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

4 个答案:

答案 0 :(得分:15)

不幸的是,这似乎是一个与Classes单元中的终结部分的调用顺序相关联的错误:

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

<强>更新
现在QC report中有一个变通方法补丁

答案 1 :(得分:12)

在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.

答案 2 :(得分:5)

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.

答案 3 :(得分:0)

我认为CurrentThread是在2009年(或2007年)添加的。我2006年在家。但你确定它是一个阶级财产吗?