On Friday, 14 June 2024 at 07:52:35 UTC, Dukc wrote:
Lance Bachmeier kirjoitti 14.6.2024 klo 4.23:
We must be talking about different things. You could, for instance, call a function in a C library to allocate memory at runtime. That function returns a pointer and you pass it to SafeRefCounted to ensure it gets freed. Nothing is known about the allocation at compile time. This is in fact my primary use case - allocating an opaque struct allocated by a C library, and not wanting to concern myself with freeing it when I'm done with it.

Using a raw pointer as the `SafeRefCounted` type like that isn't going to work. `SafeRefCounted` will free only the pointer itself at the end, not the struct it's referring to. If you use some sort of RAII wrapper for the pointer that `free`s it at it's destructor, then it'll work - maybe that's what you meant.

See the example I posted elsewhere in this thread: https://forum.dlang.org/post/mwerxaolbkuxlgfep...@forum.dlang.org

I defined

```
@nogc ~this() {
  free(ptr);
  printf("Data has been freed\n");
}
```

and that gets called when the reference count hits zero.

Reply via email to