Destructors can't be @nogc?
Paul Backus
snarwin at gmail.com
Sat Jul 24 02:48:51 UTC 2021
On Saturday, 24 July 2021 at 02:02:00 UTC, Mike Parker wrote:
> On Friday, 23 July 2021 at 20:24:02 UTC, Jim wrote:
>
>> What is the problem here? Should I not call `destroy`? If so,
>> what should I call instead?
>
> The problem is that you've marked main as `@nogc`, and
> `destroy` is not `@nogc`. Remove the annotation from main and
> it will compile.
Which raises the question, *why* is `destroy` not `@nogc` when
the destructor is `@nogc`? And it turns out the answer is that
[it calls `rt_finalize`][1], which [takes its argument as a
`void*`][2] and therefore has to assume that any destructor it
calls *might* use the GC.
[1]:
https://github.com/dlang/druntime/blob/v2.097.1/src/object.d#L3970
[2]:
https://github.com/dlang/druntime/blob/v2.097.1/src/rt/lifetime.d#L1438
More information about the Digitalmars-d-learn
mailing list