How check if destructor has been called?
Ben Jones
fake at fake.fake
Tue Sep 13 17:13:13 UTC 2022
On Tuesday, 13 September 2022 at 14:06:42 UTC, Injeckt wrote:
> Hi, I'm trying to check if destructor has been called, but when
> I'm deleting class object I didn't get any calls from
> destructor.
>
> myclass.d
>
> ~this() {
> this.log("\nDestructor\n");
> this._free_trash();
> }
>
>
> main.d
>
> try {
> server.server_init(server);
> } catch (Exception e) {
> server.log(e.msg);
> server = null;
> }
Classes are allocated on the GC heap, so even though you're
setting the reference to null, it's not being collected at that
point. You can use `destroy` to make sure the destructor is
called (see here: https://dlang.org/spec/class.html#destructors)
Or you could make your instance `scope` and then it basically
follows the same lifetime rules as `struct`s.
More information about the Digitalmars-d-learn
mailing list