Wednesday, 17 June 2020
  2 Replies
  1.3K Visits
  Subscribe
Hello,
I am using TsgcWebSocketClient(A) and TsgcWSPClient_MQTT(B) in a multi-threaded application.
I am having some troubles destroying connection. I explain a bit:
- There is a Father thread that subscribe to a Topic (Here I have one instance of both components A and B)
- The Father thread create some children threads that publish to another topic (Here I have one instance of both components A and B)
- Components A and B are created at runtime without AOwner (Create(nil))
- When the Father thread finishes A and B are destroyed correctly
- When the children threads are destroyed then problems starts. A and B are destroyed but FastMM4 (I use it for debug and deleak) says in its log this:

---- FastMM4 ----
FastMM has detected an attempt to call a virtual method on a freed object. An access violation will now be raised in order to abort the current operation.

Freed object class: TsgcWSPClient_MQTT

Virtual method: Offset +72

Virtual method address: D9BF20

The allocation number was: 145395
---- Finish FastMM4 -----

So, it says I am destroying something still freed. And this is not real because The instaces of A and B are per-thread and the free is written the same as for Father Thread.

As you can see it's a bit hard to reproduce the problem, so I hope you can have a small idea about this.
It looks really like an unsafe thread problem during the destroy. Is there any singleton or concurrent resource between components?

Another strange behavior about threads is the "OnPing" event. Wherever I create the instance of A or B, then the OnPing event always works on Main Thread of Application. I mean that the instances of A and B inside a child thread, and with OnPing event assigned with a Log function that writes the TThread.currentThread, the logged ID is always the ID of the Main Thread of the application. Why?


Thanks in advance,
Eddy