GC BUG: Referenced object destroyed before released

Graham St Jack grahams at acres.com.au
Mon Mar 17 21:32:42 PDT 2008


On Mon, 17 Mar 2008 12:27:15 +0000, Graham St Jack wrote:

> On Mon, 17 Mar 2008 10:16:57 +0100, Frits van Bommel wrote:
> 
>> Graham St Jack wrote:
>>> Another question along these lines - I am having trouble with a multi-
>>> threaded program (linux, gdc, phobos). It works fine until main()
>>> returns, and then I get a SIGUSR1 that causes the program to crash.
>>> Any ideas what might be happening?
>>> 
>>> My investigations so far show that SIGUSR1 and SIGUSR2 are used in
>>> thread.d as part of the implementation of pause() and resume(). I was
>>> under the impression that the thread that is interrupted to handle a
>>> signal is chosen at random, and therefore that it is a bad idea to try
>>> to use signals in a threaded program, so I am confused.
>>> 
>>> Also, the garbage collector seems to kill off all my threads without
>>> waiting for their run methods to return. I assume this means that the
>>> threads aren't being treated as root objects by the garbage collector.
>> 
>> When main() exits, the GC starts a collection. In order to do so safely
>> it pauses all other threads with Thread.pauseAll, which (in the Linux
>> version) sends SIGUSR1 to all other threads to tell them to pause.
>> (Note that this doesn't kill them, even though the function that does
>> this is called pthread_kill)
>> It's usually a good idea to just tell your debugger to ignore SIGUSR1
>> and SIGUSR2 (the latter is used to resume afterwards). To do this in
>> GDB, use "handle SIGUSR1 noprint nostop SIGUSR2 noprint nostop".
>> 
>> If you're just wondering why all your threads get killed when main()
>> ends I can tell you right now: this is how it works :P. You should see
>> the same in a C or C++ program.
>> 
>>> Does all this mean that I have to explicitly take action so that the
>>> run methods will return, then wait() on them before returning from
>>> main()?
>> 
>> As you said, call Thread.wait() on all other threads before exiting
>> main() to wait for them to end. Or just switch to Tango which does this
>> automatically (add Tangobos if you need Phobos compatibility).
> 
> Thanks for the information - it has saved me a lot of wasted time.

Just to let you know, I fixed my code along these lines and it works just 
fine. Thanks for the leg up.



More information about the Digitalmars-d mailing list