What should happen here?

Paulo Pinto pjmlp at progtools.org
Thu Sep 23 19:29:32 UTC 2021


On Thursday, 23 September 2021 at 18:41:55 UTC, Steven 
Schveighoffer wrote:
> On 9/23/21 1:45 PM, Johan wrote:
>> On Thursday, 23 September 2021 at 12:47:25 UTC, Steven 
>> Schveighoffer wrote:
>>>
>>> I think we will need a real compiler intrinsic at this point.
>> 
>> For LDC (and I expect GDC too), the asm trick works.
>
> oooh, really? That's cool. Maybe I'll update the library and 
> re-register.
>
> Looking at the disassembly, I do see the difference. This is it 
> pushing to the stack, right?
>
> ```asm
>  mov     qword ptr [rsp], rax
> ```
>
> -Steve

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

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

I think the mistake was trying to force c to exist on the stack, 
but without being used.

-- Paulo


More information about the Digitalmars-d mailing list