On Monday, 24 May 2021 at 16:16:53 UTC, Steven Schveighoffer wrote:


Nice article!

Thanks!


Note that there is a huge pitfall awaiting you if you use `toStringz`: garbage collection. You may want to amend the article to identify this pitfall.

And I'm not talking about requiring `@nogc`, I'm talking about the GC collecting the data while C is still using it.

In your example:

```d
puts(s1.toStringz());
```

This leaves a GC-collectible allocation in C land. For `puts`, it's fine, as the data is not used past the call, but in something else that might keep it somewhere not accessible to the GC, you'll want to assign that to a variable that lasts as long as the resource is used.


That's what I'm referring to in the conclusion where I say this about what's going to be in Part Two:

how to avoid a potential problem spot that can arise when passing GC-allocated D strings to C

I'll cover approaches to maintaining a reference, like `GC.addRoot`, and emphasize that it applies to any GC-allocated memory, not just strings.



              • ... rikki cattermole via Digitalmars-d-announce
              • ... zjh via Digitalmars-d-announce
              • ... Vinod K Chandran via Digitalmars-d-announce
              • ... zjh via Digitalmars-d-announce
              • ... dangbinghoo via Digitalmars-d-announce
              • ... zjh via Digitalmars-d-announce
  • Re: From the D Blog... sighoya via Digitalmars-d-announce
  • Re: From the D Blog... Steven Schveighoffer via Digitalmars-d-announce
    • Re: From the D... John Colvin via Digitalmars-d-announce
    • Re: From the D... Mike Parker via Digitalmars-d-announce
      • Re: From t... Steven Schveighoffer via Digitalmars-d-announce
        • Re: Fr... surlymoor via Digitalmars-d-announce
          • Re... Steven Schveighoffer via Digitalmars-d-announce
        • Re: Fr... Mike Parker via Digitalmars-d-announce
  • Re: From the D Blog... Виталий Фадеев via Digitalmars-d-announce

Reply via email to