GC seems to crash my C-code function

Steven Schveighoffer schveiguy at gmail.com
Thu Sep 16 15:34:25 UTC 2021


On 9/16/21 6:28 AM, frame wrote:
> I have C-code translated in D that acts sometimes incorrect if the GC 
> has made some collect. I would like to know why.
> 
> - Code runs correct if the GC collections are off
> - There are no allocations within the C-translated-code except `throw 
> new` (but they are not called)

...

> I didn't want to change the code much so I have some piece like that:
> ```d
> // const char *s2 = "!\"#$%&'()*+,-./:;<=>?@[\\]^_";
> char* s2 = "!\"#$%&'()*+,-./:;<=>?@[\\]^_".dup.ptr;
> ```
> Could this cause the issue? But the pointer is not used outside the 
> function where it's created.

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

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.

-Steve


More information about the Digitalmars-d-learn mailing list