More fun with toStringz and the GC
Steven Schveighoffer
schveiguy at gmail.com
Fri Aug 5 23:38:22 UTC 2022
On 8/5/22 7:13 PM, jfondren wrote:
> On Friday, 5 August 2022 at 22:51:07 UTC, Don Allen wrote:
>> My theory: because gc_protect2 is never referenced, I'm guessing that
>> the compiler is optimizing away the storage of the returned pointer,
>> the supporting evidence being what I said in the previous paragraph.
>> Anyone have a better idea?
>
> A local variable definitely isn't enough:
> https://forum.dlang.org/thread/xchnfzvpmxgytqprbosz@forum.dlang.org
>
> This package came of it: https://code.dlang.org/packages/keepalive
>
Yes, but I will warn you, the compilers are smart buggers. I think
someone came up with a case where this still doesn't keep it alive (been
a while since I made that).
The only true solution is to use `GC.addRoot` on the string and
`GC.removeRoot` when you are done.
-Steve
More information about the Digitalmars-d-announce
mailing list