More fun with toStringz and the GC

H. S. Teoh hsteoh at qfbox.info
Sat Aug 6 13:37:30 UTC 2022


On Fri, Aug 05, 2022 at 10:14:24PM -0400, Steven Schveighoffer via Digitalmars-d-announce wrote:
> On 8/5/22 8:51 PM, Don Allen wrote:
> 
> > And this, from Section 32.2 of the Language Reference Manual:
> > ````
> > If pointers to D garbage collector allocated memory are passed to C
> > functions, it's critical to ensure that the memory will not be
> > collected by the garbage collector before the C function is done
> > with it. This is accomplished by:
> > 
> >      Making a copy of the data using core.stdc.stdlib.malloc() and
> >  passing the copy instead.
> >      -->Leaving a pointer to it on the stack (as a parameter or
> > automatic variable), as the garbage collector will scan the stack.<--
> >      Leaving a pointer to it in the static data segment, as the garbage
> > collector will scan the static data segment.
> >      Registering the pointer with the garbage collector with the
> > std.gc.addRoot() or std.gc.addRange() calls.
> > ````
> > I did what the documentation says and it does not work.
> 
> I know, I felt exactly the same way in my post on it:
> 
> https://forum.dlang.org/post/sial38$7v0$1@digitalmars.com
> 
> I even issued a PR to remove the problematic recommendation:
> 
> https://github.com/dlang/dlang.org/pull/3102
> 
> But there was pushback to the point where it wasn't worth it. So I
> closed it.
[...]

IMO this PR should be revived. The one thing worse than no documentation
is misleading documentation.  This state of things should not be allowed
to continue.


T

-- 
Study gravitation, it's a field with a lot of potential.


More information about the Digitalmars-d-announce mailing list