在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.. 15
不幸的是,它似乎是一个与Classes单元中finalization部分的调用顺序相关联的bug:
DoneThreadSynchronization
清除ThreadLock
结构,然后
FreeExternalThreads
想要销毁刚刚调用时创建的Thread对象CurrentThread
,
这需要在调用
EnterCriticalSection(ThreadLock)
中初始化ThreadLock TThread.RemoveQueuedEvents
...
更新:
现在有一个解决办法补丁的QC报告.
不幸的是,它似乎是一个与Classes单元中finalization部分的调用顺序相关联的bug:
DoneThreadSynchronization
清除ThreadLock
结构,然后
FreeExternalThreads
想要销毁刚刚调用时创建的Thread对象CurrentThread
,
这需要在调用
EnterCriticalSection(ThreadLock)
中初始化ThreadLock TThread.RemoveQueuedEvents
...
更新:
现在有一个解决办法补丁的QC报告.
在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.
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.