GC seems to crash my C-code function

Steven Schveighoffer schveiguy at gmail.com
Fri Sep 17 14:29:23 UTC 2021


On 9/17/21 2:27 AM, frame wrote:
> On Thursday, 16 September 2021 at 18:02:44 UTC, Steven Schveighoffer wrote:
> 
>> 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.
> 
> If you want to have a look the original C-library is here
> https://github.com/rdoeffinger/iec16022
> 
> I'm only using the encoder function iec16022ecc200f.

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?

What does your call look like?

> 
>>
>> 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 *`).
>>
> 
> Thanks, I'm just careful with casting.
> Does it really allocate from a literal if it's used on the stack only? 
> Is `-vgc` switch reliable?

The `-vgc` switch appears to only identify allocations that the compiler 
invokes via hooks, not ones that other functions invoke (or ones that 
are direct calls into the GC).

In other words, it helps you find your direct allocations using the 
compiler, not ones that are buried in already-compiled code.

Try this with `-vgc`, and it reports nothing:

```d
import core.memory;
void main()
{
    auto x = GC.malloc(10);
}
```

This makes sense, as it may not have the function code to analyze, and 
it also cannot infer GC allocation from a lack of @nogc (a non- at nogc 
function *may* allocate, but does not *necessarily* allocate). It also 
would be quite useless to see some function buried inside druntime that 
allocates, not knowing what the call stack was.

The docs for `-vgc` should really be updated to clarify. It currently 
just says "List all gc allocations including hidden ones".

-Steve


More information about the Digitalmars-d-learn mailing list