TMS Async: v1.5.0 (also tested v1.9.2)
IDE: Delphi XE2 Update 4 (also tested Delphi 7)
Problem Summary: An exception is raised while serial input is being processed in VaComm.OnRxChar
from a COM port. The exception is unhandled so gets caught by Application.OnException
where I attempt to VaComm.Close()
the port, but this makes the program freeze (not crash).
Here is the step-by-step:
1) An ERangeError
exception is raised in TfrmUartComm.OnSpecificRES()
. This is what the call stack looks like at this point:
-> :751cc42d KERNELBASE.RaiseException + 0x58
:006ca67e HookedRaiseException + $5E
:0040995a DynArraySetLength + $2A
:0040995a DynArraySetLength + $2A
:00409ad6 @DynArraySetLength + $A
UartComm.TfrmUartComm.OnSpecificRES($2259530,$10B543E0)
UartComm.TfrmUartComm.OnSpecificPktRX($220A920,$10B543E0)
UartComm.TfrmUartComm.DisplayUartFrame($220A920,$10B543E0,0)
UartComm.TfrmUartComm.UartVaComm1RxChar($220A920,0)
VaComm.TVaCustomComm.HandleDataEvent
VaComm.TVaCommEventThread.DoEvent
2) The exception is unhandled so it falls all the way to VaComm.TVaCommEventThread.DoEvent()
. There it's caught by the line:
Application.HandleException(Self);
which calls the routine Application.OnException
is bound to, in my case LogExceptions.ExceptionHandler()
.
3) In LogExceptions.ExceptionHandler()
I run some wrapper code that eventually closes the VaComm1: TVaComm
object which is contained in frmConnection: TForm
:
procedure TfrmConnection.Disconnect;
begin
if VaComm1.Active then
VaComm1.Close();
end;
4) VaComm1.Close()
calls VaComm.TVaCustomComm.DestroyThreads()
and the program freezes on the marked line:
procedure TVaCustomComm.DestroyThreads;
begin
if FEventThread <> nil then
begin
FEventThread.StopThread;
FEventThread.WaitFor; // <-- FREEZES WHEN IT EXECUTES THIS LINE.
Here is the callstack at this point:
-> VaComm.TVaCustomComm.DestroyThreads
VaComm.TVaCustomComm.Close
Connection.TfrmConnection.Disconnect
UartComm.TfrmUartComm.DisconFromCOMPort
Main.TfrmMain.Disconnect
LogExceptions.TLogExceptions.ExceptionHandler($22F09E0,$10BA0ED8)
:00583438 TApplication.HandleException + $64
Note that it doesn't crash; there's no error. Execution continues forever and the GUI is frozen.
If I use the IDE to "Tools -> Program Pause" it shows the CPU window is executing:
ntdll.RtlUserThreadStart
then it executes:
ntdll.RtlInitializeExceptionChain:
Every time I run and re-pause it, it shows the same thing.
Also note that at the time ERangeError
exception is raised, no more bytes are being received on the serial connection: so communication isn't active.
I don't understand why this problem happens or how to fix it. Let me know if you need more info.