Destructors can't be @nogc?

Jim jim at mailinator.com
Fri Jul 23 20:24:02 UTC 2021


Hello,

I've been playing with D and trying to understand how to work 
with @nogc. I must be doing something wrong, because even though 
I tagged the destructor for my class `@nogc`, I'm getting the 
following error: `.\min.d(27): Error: "@nogc" function "D main" 
cannot call non- at nogc function "object.destroy!(true, 
TestClass).destroy`

```D
import std.stdio : printf;
import core.lifetime : emplace;
import core.stdc.stdlib : malloc, free;

class TestClass {
     int x;

     this(int x) @nogc {
         printf("TestClass's constructor called\n");
         this.x = x;
     }


     ~this() @nogc {
         printf("TestClass's destructor called\n");
     }
}

@nogc void
main() {
     auto size = __traits(classInstanceSize, TestClass);
     auto memory = malloc(size)[0..size];
     TestClass x = emplace!(TestClass)(memory, 1);

     printf("TestClass.x = %d\n", x.x);

     destroy(x);
     free(cast(void*)x);
}

```

What is the problem here? Should I not call `destroy`? If so, 
what should I call instead?




More information about the Digitalmars-d-learn mailing list