Question

I have come across an apparent handle leakage using the Borland/Embarcadero TClientSocket component. I have an application that creates multiple TThread instances, each of which creates a TClientSocket object dynamically, connects to its target sends a few messages and then is deleted. The TThread instances are then deleted (using the FreeOnTerminate = true) setting. I know that this is inefficient, but it suits the needs of the application perfectly - the maximum number of TThread instances capable of existing simultaneously are limited to 32. The problem I am seeing is that there is a clear windows Handle leakage problem that I can see via Task Manager. In an attempt to isolate the problem I applied the same problem in a single threaded sense, just dynamically creating a TClientSocket object and deleting it again when finished with within a single loop in the main VCL thread. This exhibits the same handle leakage problem. I know the component is deprecated and I know that what I am doing is inefficient, but I cannot see why there would be a handle leak. Is there anything that needs to be done with the TClientSocket objects prior to their deletion to remove this handle leak, or is this a bug in the component? I am using the socket in non blocking mode and assigning event handlers to OnConnect OnDisconnect and OnSocketError.

Était-ce utile?

La solution

I have used TClientSocket for many years, including usages in the main thread and in worker thread, and I have never seen TClientSocket leak any handles.

However, TClientSocket does default to non-blocking mode, and in that mode it uses AllocateHWnd() to create a hidden window to receive socket events, and AllocateHWnd() is not thread-safe. Without seeing your actual code, that is the likely cause of the leaks you are seeing in your worker thread code. The solution to that is simply to not use TClientSocket is non-blocking mode when used in a worker thread. Use it in blocking mode instead. Which lends itself better to thread-based logic anyway.

However, that does not explain the leaks you are seeing in your main thread code. I doubt TClientSocket is actually the culprit, and again, without seeing your actual code, it is hard to say for sure.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top