On 7/16/23 2:41 PM, Alain De Vos wrote:
Is this ok ?
```
void main(){
     int[] i=new int[10000];
     import object: destroy;
     destroy(i);
     import core.memory: GC;
     GC.free(GC.addrOf(cast(void *)(i.ptr)));
}
```

No, that won't work. Check out `i` value after you call `destroy` on it:

```d
destroy(i); // basically sets i = null
assert(i.ptr is null); // yep
GC.free(i.ptr); // basically free(null) which is a no-op
```

Also note that `destroy` is *shallow*. It does not dig into pointers or arrays. So even if your array was of elements with a destructor, destroying the array doesn't destroy the elements.

In this case, all you need to do is:

```d
GC.free(GC.addrOf(i.ptr));
```
You don't need the cast here. You shouldn't need the addrOf, but this is still open: https://issues.dlang.org/show_bug.cgi?id=13558

-Steve
  • Re: How to free ... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
    • Re: How to ... Alain De Vos via Digitalmars-d-learn
      • Re: How... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
      • Re: How... Steven Schveighoffer via Digitalmars-d-learn
        • Re:... Alain De Vos via Digitalmars-d-learn
          • ... Alain De Vos via Digitalmars-d-learn
            • ... Steven Schveighoffer via Digitalmars-d-learn
          • ... Steven Schveighoffer via Digitalmars-d-learn
        • Re:... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
          • ... Alain De Vos via Digitalmars-d-learn
            • ... drug007 via Digitalmars-d-learn
              • ... Alain De Vos via Digitalmars-d-learn
                • ... drug007 via Digitalmars-d-learn
              • ... Alain De Vos via Digitalmars-d-learn

Reply via email to