[Lazarus] Tracking down an intermittent termination exception

Mark Morgan Lloyd markMLl.lazarus at telemetry.co.uk
Tue Mar 26 12:16:24 CET 2013


Sven Barth wrote:

>>   DoneCriticalSection(SynchronizeCritSect);
>>
>> Assuming that this is a problem in my own code, what sort of thing 
>> should I be looking for?
> 
> After looking through the following things:
> 
> - code of CDoneCriticalSection (in rtl/unix/cthreads.pp)
> - man page of pthread_mutex_destroy
> - code of fpc_threaderror (in rtl/inc/system.inc)
> - code of RunErrorToExcept (in rtl/objpas/sysutils/sysutils.inc)
> 
> 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.

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.

> 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.

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?

-- 
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]




More information about the Lazarus mailing list