On 9/16/21 1:08 PM, frame wrote:
On Thursday, 16 September 2021 at 15:34:25 UTC, Steven Schveighoffer wrote:

`dup` is a GC allocation. Are you using that in your C code? the GC might be collecting that string.

The compiler doesn't show that lines with -vgc. Maybe it knows that it is only stack allocated?

Technically, the GC could collect that data if it wants - it's not longer used after the function returns. At least I control that the GC cannot collect it till my data is processed, so there should be no problem.

Are you sure? Be very pedantic about what C functions do with the data you send it. Sometimes they store it somewhere to use later. Sometimes they expect it to be allocated by the C heap, etc.

Without seeing how you use it, I can't tell you if it's wrong or not.


I guess the C-methods did corrupt the memory which the compiler has reserved for that function data statically and then the GC collect marks it as free or some other UB.


You are better off to cast away the immutable (as long as you are 100% sure the C code isn't writing to it), as the string literal will not be collected.


Yes, I changed it to stringz and a cast and the problem is gone so far. All char* are read only in the function or passed to stdlib functions that should not modify it.



If it's a literal, you don't need to toStringz (which also allocates). All string literals are zero-terminated (and actually implicitly castable to `immutable char *`).

-Steve

Reply via email to