Either I'm confused or the gc is

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Nov 16 19:26:17 UTC 2020

On Sun, Nov 15, 2020 at 01:46:52PM -0800, Ali Çehreli via Digitalmars-d wrote:
> While I have everbody's attention, :) toStringz will come up during
> one of my DConf presentations. I will claim that it is safe to pass to
> a C function for their immediate consumption (but they can't save the
> pointer).
> So, I will claim that the following is safe and no GC collection on
> another thread can mess this up. (The C side is not allowed to store
> for later use but they are taking the pointer into a local variable on
> their function call stack.)
> nothrow extern(C) void bar(const(char) ** name) {
>   // ...
>   *name = makeString(42).toStringz;
>   // ...
> }

AFAIK, as long as a reference exists on the stack, you should be safe,
because the GC does scan the stack.  But if the C side stores it
anywhere else past the point where the on-stack reference goes out of
scope, then you're in trouble.


Chance favours the prepared mind. -- Louis Pasteur

More information about the Digitalmars-d mailing list