[Dlang-study] [lifetime] Initial thoughts on lifetime management
Walter Bright
walter at digitalmars.com
Wed Oct 28 13:01:23 PDT 2015
On 10/28/2015 11:00 AM, Timon Gehr wrote:
>
>
> Anyway, the solution is indeed to make it the responsibility of the caller to
> keep the borrowed object alive. I.e. when passing a non-scope reference to a
> scope argument, the reference count must be increased for the duration of the
> call. The benefit is that scope references can be duplicated arbitrarily
> without updating the reference count.
> This applies to globals just as well as to other cases.
Another option is, when doing an assignment to an rc handle, and the ref count
goes to zero, to not delete the rc object, but to hand it to the garbage
collector to eventually reap.
The advantage to this is you never have to inc/dec a ref count when passing a
handle to a function. The downside is, of course, eventually the GC will have to
run to reap them. But this may not be much of a problem in practice, as it can
only happen on assignment (not initialization) of a non-local handle, and only
happens if the ref count goes to 0.
More information about the Dlang-study
mailing list