Thread Exception on Terminate
JamardaDigital opened this issue · 3 comments
I was getting an intermittent thread exception on termination. I fixed by moving:
FThread.FreeOnTerminate := False;
from TDosCommand.Stop and placed it after the thread is created in TDosCommand.Execute.
This seems to fix the issues I've been having. Can someone please confirm.
Do you have a demo app to reproduce the issue?
No sorry, my current project is HUGE and it's hard to pull out the bits to make a simple demo. About every other run or so it produced an exception with invalid thread handle. This clued me to the fact that the thread object may have already been released. Looking at FreeOnTerminate it looks like its modifies the object ref count. So it makes sense to call FreeOnTerminate := False right after object creation and before Execute rather than in Stop. Since making this change, I've not seen the exception again.
The code doesn't seem to be threadsafe. That's why I moved the FreeOnTerminate := False to the constructor of TDosThread.
Could you please check this?