Preventing nested struct destructor accessing stack frame

Steven Schveighoffer schveiguy at gmail.com
Fri Dec 23 16:36:31 UTC 2022


On 12/16/22 7:17 AM, Nick Treleaven wrote:
> This code segfaults when the GC calls the dtor after the unittest succeeds:
> 
> ```d
> unittest
> {
>      int i;
>      struct S
>      {
>          ~this() { i++; }
>      }
>      (*new S).destroy;
> }
> ```
> 
> It seems destroy clears the context pointer. Is there a way to test if 
> the context pointer is null in the dtor, to prevent the increment?

Check if the struct is the init value?

```d
~this() { if(this !is this.init) ++i;}
```

Not ideal I guess, because really it's the context pointer you care about.

-Steve


More information about the Digitalmars-d-learn mailing list