<p>Am 26.03.2013 12:16 schrieb "Mark Morgan Lloyd" <<a href="mailto:markMLl.lazarus@telemetry.co.uk">markMLl.lazarus@telemetry.co.uk</a>>:<br>
><br>
> Sven Barth wrote:<br>
><br>
>>> DoneCriticalSection(SynchronizeCritSect);<br>
>>><br>
>>> Assuming that this is a problem in my own code, what sort of thing should I be looking for?<br>
>><br>
>><br>
>> After looking through the following things:<br>
>><br>
>> - code of CDoneCriticalSection (in rtl/unix/cthreads.pp)<br>
>> - man page of pthread_mutex_destroy<br>
>> - code of fpc_threaderror (in rtl/inc/system.inc)<br>
>> - code of RunErrorToExcept (in rtl/objpas/sysutils/sysutils.inc)<br>
>><br>
>> the most valid assumption is that the critical section is destroyed while it's in use. As this critical section is only used inside a Synchronize call from a thread (the mainthread will directly call the method without any locking) it seems that your background thread is inside a Synchronize call when you terminate your application.<br>
><br>
><br>
> Synchronize() is only used for message display, the program's silent until it sees TCP traffic. From what you're saying I suspect that it's probably caused by the socket the thread's waiting on being forcibly closed, at which point the thread tries to display a message.<br>
></p>
<p>Yes, that could he the problem then.</p>
<p>>> Solution: Tell the thread to terminate (if you use the thread's Terminate property you'll need to check this inside your thread's loop) and then wait for (TThread.WaitFor) it to finish before you continue with termination. This will also ensure that any cleanup that is done inside the thread is finished before the RTL is finalized.<br>
><br>
><br>
> Thanks Sven, I'll dig. At present the background thread is watching for the main thread to start terminating as a stimulus for it to fall through the main loop, is there a wiki page or an example somewhere showing "best practice" for this sort of thing?<br>
></p>
<p>I don't know whether there is something like that... :(</p>
<p>Regards,<br>
Sven</p>