GC seems to crash my C-code function

Steven Schveighoffer schveiguy at gmail.com
Sat Sep 18 11:47:52 UTC 2021


On 9/18/21 5:40 AM, frame wrote:
> On Friday, 17 September 2021 at 14:29:23 UTC, Steven Schveighoffer wrote:
> 
>> Looking at that signature, it does not appear that it uses 
>> zero-termination at all, as it takes a length. So using `dup` and 
>> therefore the gc is totally unnecessary.
>>
>> I'm assuming that string is the barcode argument?
> 
> No, the string appears inside the C-function. I'm calling the function 
> with .ptr and the .length property as it expects.

Oh wow, I totally misunderstood what you are doing. I thought you were 
*calling* that function, but you are *reimplementing* that code in D.

Have you tried:

```d
const(char)* s2 = "...";
```

This will work because string literals are zero terminated and 
implicitly castable to `immutable(char)*`, which will also implicitly 
cast to `const(char)*`.

That should allow s2 to be reassigned but not modify the data. IIRC, 
string literal data even in C is put into a read-only section by many 
compilers, so the code shouldn't be changing it.

Now that I understand what you are doing, it becomes clear that this 
isn't a situation of C code being called. Or are you calling other parts 
of the C library with that translated function?

The first rule of porting -- just translate, don't change anything. I 
would try to do exactly what C does without using the GC at all. 
Continue to use malloc/free. If you have issues with type 
representation, you may need to adjust for that.

-Steve


More information about the Digitalmars-d-learn mailing list