TurboPack / AsyncPro

Async Professional is a comprehensive communications toolkit for Embarcadero Delphi and C++Builder.
101 stars 51 forks source link

Number of handles increases after repeated attempts to open an unavailable COM port #30

Closed michaelr1966 closed 2 years ago

michaelr1966 commented 2 years ago

We use a service that serves various serial scales. The COM interfaces are provided via the network via COM servers from W&T (Wiesemann & Theis GmbH). If, for example, this COM interface is not available in a test environment, the exception "Apro exception" occurs.

Rec=-995

image

In my opinion, the reason are the 6 events (ComEvent, ReadyEvent, GeneralEvent, OutputEvent, SentEvent and OutFlushEvent) which are generated but not closed again in such a case.

image

romankassebaum commented 2 years ago

When do get the exception? How does the callstack look like? Have you tried to catch the exception and to close the handle explicitly?

michaelr1966 commented 2 years ago

"When do get the exception?"

By open the port.

image

"How does the callstack look like?"

20220425-085055

"Have you tried to catch the exception and to close the handle explicitly?"

Yes, with the code above. The number of handles still increases by 6.

romankassebaum commented 2 years ago

Can you find out in which line exactly in the Method InitPort the exception happens?

As a workaround could you please try the following code:

{code} uses AdExcept, AwUser;

type TProtectedApdComPort = class(TApdComPort) end; TProtectedApdDispatcher = class(TApdBaseDispatcher) end;

procedure TForm1.Button1Click(Sender: TObject); begin Button1.Enabled := False; try ApdComPort1.ComNumber := 2; ApdComPort1.Baud := 115200; ApdComPort1.DataBits := 8; ApdComPort1.StopBits := 1; ApdComPort1.Parity := pNone; ApdComPort1.HWFlowOptions := []; ApdComPort1.SWFlowOptions := swfNone; try ApdComPort1.Open := True; except on E: EAPDException do begin CloseHandle(TProtectedApdDispatcher(TProtectedApdComPort(ApdComPort1).FDispatcher).ComEvent); CloseHandle(TProtectedApdDispatcher(TProtectedApdComPort(ApdComPort1).FDispatcher).ReadyEvent); CloseHandle(TProtectedApdDispatcher(TProtectedApdComPort(ApdComPort1).FDispatcher).GeneralEvent); CloseHandle(TProtectedApdDispatcher(TProtectedApdComPort(ApdComPort1).FDispatcher).OutputEvent); CloseHandle(TProtectedApdDispatcher(TProtectedApdComPort(ApdComPort1).FDispatcher).SentEvent); CloseHandle(TProtectedApdDispatcher(TProtectedApdComPort(ApdComPort1).FDispatcher).OutFlushEvent); end; end; finally Button1.Enabled := True; end; end; {code}

michaelr1966 commented 2 years ago

Here is the location were the value of -995 is set.

image

In the workaround code "TProtectedApdComPort(ApdComPort1).FDispatcher" is nil

image

romankassebaum commented 2 years ago

I hope I could fix the issue. Could you please try?

michaelr1966 commented 2 years ago

I have tested it. That solved the problem. Thank you!