Program crash: GC destroys an object unexpectedly

eugene dee0xeed at gmail.com
Tue Sep 21 20:17:15 UTC 2021


On Tuesday, 21 September 2021 at 19:42:48 UTC, jfondren wrote:
> On Monday, 13 September 2021 at 17:18:30 UTC, eugene wrote:
> There's nothing special about sg0 and sg1, except that they're 
> part of Stopper. The Stopper in main() is collected before the 
> end of main() because it's not used later in the function

Okay, but how could you explain this then

```d
void main(string[] args) {

     auto Main = new Main();
     Main.run();

     auto stopper = new Stopper();
     stopper.run();
```

```
d-lang/edsm-in-d-simple-example-2 $ ./test | grep STOPPER
'STOPPER' registered 5 (esrc.Signal)
'STOPPER' registered 6 (esrc.Signal)
'STOPPER @ INIT' got 'M0' from 'SELF'
'STOPPER' enabled 5 (esrc.Signal)
'STOPPER' enabled 6 (esrc.Signal)
___!!!___edsm.StageMachine.~this(): STOPPER destroyed...
    !!! esrc.EventSource.~this() : esrc.Signal (owner STOPPER, fd 
= 5) this @ 0x7fc9ab1a9150
    !!! esrc.EventSource.~this() : esrc.Signal (owner STOPPER, fd 
= 6) this @ 0x7fc9ab1a9180
```

Now, change operation order in the main like this:

```d
void main(string[] args) {

     auto Main = new Main();
     auto stopper = new Stopper();

     Main.run();
     stopper.run();
```

```
d-lang/edsm-in-d-simple-example-2 $ ./test | grep STOPPER
'STOPPER' registered 5 (esrc.Signal)
'STOPPER' registered 6 (esrc.Signal)
'STOPPER @ INIT' got 'M0' from 'SELF'
'STOPPER' enabled 5 (esrc.Signal)
'STOPPER' enabled 6 (esrc.Signal)
```

Everything is Ok now, stopper is not collected soon after start.
So the question is how this innocent looking change
can affect GC behavior so much?...

> Misaligned pointers are one way to hide objects from the GC but 
> in this case they really weren't relevant.

For sure.




More information about the Digitalmars-d-learn mailing list