question about keeeping reference to toStringz()

Dr.No jckj33 at gmail.com
Wed May 30 20:09:18 UTC 2018


The documentation says:

Important Note: When passing a char* to a C function, and the C 
function keeps it around for any reason, make sure that you keep 
a reference to it in your D code. Otherwise, it may become 
invalid during a garbage collection cycle and cause a nasty bug 
when the C code tries to use it.

(from https://dlang.org/library/std/string/to_stringz.html)

consider a C function with this prototype:
>void foo(const char *baa);

Does it means I should do:

> string s = ...;
> auto cstring = s.toStringz;
> foo(cstring);

rather just:

> foo(s.toStringz);

?


More information about the Digitalmars-d-learn mailing list