want to confirm: gc will not free a non-gc-allocated field of a gc-allocated object?

mw mingwu at gmail.com
Mon Jun 6 22:18:08 UTC 2022


Hi,

Suppose I have this code:

```
class GCAllocated {
   float[] data;

   this() {
     // non-gc-allocated field
     this.data = cast(float[])(core.stdc.stdlib.malloc(nBytes)[0 
.. nBytes]);
   }
}

void foo() {
   auto obj = new GCAllocated();  // gc-allocated owning object
   ...
}

```

So when `obj` is cleanup by the GC, obj.data won't be freed by 
the GC: because the `data` is non-gc-allocated (and it's 
allocated on the non-gc heap), the GC scanner will just skip that 
field during a collection scan. Is this understanding correct?

I need this behavior for a special purpose, so I want to confirm 
it.

Thanks.



More information about the Digitalmars-d-learn mailing list