How to capture suddenly destructed TThread

141 Views Asked by At
My Environment: Rad Studio XE4 using C++

I am working on a software using TThread. Recently, I am facing a problem in which the TThread is suddenly destructed after several days from the program start.

In the TThread, the thread changes displays of forms (e.g. TLables) using Synchronize() method. I wonder this might cause some problem.

I am logging at several parts in the thread so that I can catch the cause of the problem. But when I check the log, I can only find that the destructor of the TThread is suddenly called.

The FreeOnTerminate of the TThread is set as true.

The program runs two threads. Only one thread is destructed suddenly and the other thread is working even after the problem occurs.

I am searching the way how to catch the cause of this kind of problem.

1

There are 1 best solutions below

2
On BEST ANSWER

when I check the log, I can only find that the destructor of the TThread is suddenly called.

The only way that can happen is:

  1. If you are setting the thread's FreeOnTerminate property to true (which you are) and your thread is terminating due to its Execute() method exiting (either because it encountered a return statement, reached the end of its code, or threw an exception you did not catch). To log if the thread is terminating, you can override its virtual DoTerminate() method, or assign an OnTerminate event handler to it.

  2. If some piece of code outside of the thread is calling delete on the thread object pointer. To log if this is happening, you will have to log your delete calls.

Given that you are setting FreeOnTerminate to true, then the likely culprit is Execute() exiting due to an uncaught exception.