What should happen here?

Paul Backus snarwin at gmail.com
Wed Sep 22 19:30:37 UTC 2021


On Wednesday, 22 September 2021 at 19:14:43 UTC, Steven 
Schveighoffer wrote:
> On 9/22/21 3:10 PM, Adam D Ruppe wrote:
>> 
>> One thing we might do is if passing a pointer to an extern(C) 
>> function, the compiler assumes it shouldn't stomp the memory 
>> until end of scope.
>
> What if you are calling D functions that call extern(C) 
> functions?
>
> I don't think this is the answer either.

Either (a) the compiler must assume, pessimistically, that a 
pointer passed to a function may be stored somewhere the GC can't 
see it, and therefore must be preserved in local storage until 
the end of its lexical lifetime, or (b) the user must manually 
signal to the compiler or the GC that the pointer should be kept 
alive before passing it to the function.

Given how error-prone option (b) is, I think (a) is the more 
sensible choice. Users who don't want the pointer kept alive can 
always opt-in to that behavior by enclosing it in a block scope 
to limit its lifetime, or setting it to `null` when they're done 
with it.


More information about the Digitalmars-d mailing list