What should happen here?

Steven Schveighoffer schveiguy at gmail.com
Fri Sep 24 12:14:36 UTC 2021


On 9/23/21 3:29 PM, Paulo Pinto wrote:

> It should, that is also what the keep alive from C# and Go that I post 
> earlier do.

Yes, I took a lot of inspiration from that.

> 
> You are supposed to use keepalive this way:
> 
> ~~~D
> auto c = new C;
> // ... lots of ongoing activities ...
> keepAlive(c); // c will survive until keepAlive returns.
> ~~~
> 
> If you look into their source code, they then trick to trick the 
> optimizer that keepAlive is relevant and shouldn't be taken away, thus 
> forcing the GC to ensure that the reference isn't collected until 
> function returns.
> 
> https://cs.opensource.google/go/go/+/refs/tags/go1.17.1:src/runtime/mfinal.go;l=473 

Right, what I'd prefer is something that doesn't actually require a call 
or code of any kind, but the empty asm seems to require the minimum.

Note that using my keepalive library that way (just calling c.keepAlive 
at the end of the function) will also work.

-Steve


More information about the Digitalmars-d mailing list