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.
The only way that can happen is:
If you are setting the thread's
FreeOnTerminateproperty to true (which you are) and your thread is terminating due to itsExecute()method exiting (either because it encountered areturnstatement, 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 virtualDoTerminate()method, or assign anOnTerminateevent handler to it.If some piece of code outside of the thread is calling
deleteon the thread object pointer. To log if this is happening, you will have to log yourdeletecalls.Given that you are setting
FreeOnTerminateto true, then the likely culprit isExecute()exiting due to an uncaught exception.