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

max haughton maxhaton at gmail.com
Mon Jun 6 22:22:05 UTC 2022


On Monday, 6 June 2022 at 22:18:08 UTC, mw wrote:
> 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.

float[] doesn't contain pointers, so the GC won't do anything to 
or with it.


More information about the Digitalmars-d-learn mailing list