RCArray is unsafe

Manu via Digitalmars-d digitalmars-d at puremagic.com
Tue Mar 3 23:50:37 PST 2015


On 4 March 2015 at 01:07, Zach the Mystic via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
> On Tuesday, 3 March 2015 at 08:04:25 UTC, Manu wrote:
>>
>> My immediate impression on this problem:
>>
>> s.array[0] is being passed to foo from main. s does not belong to main
>> (is global), and main does not hold have a reference to s.array.
>> Shouldn't main just need to inc/dec array around the call to foo when
>> passing un-owned references down the call tree.
>> It seems to me that there always needs to be a reference _somewhere_
>> on the stack for anything being passed down the call tree (unless the
>> function is pure). Seems simplest to capture a stack ref at the top
>> level, then as it's received as arguments to each callee, it's
>> effectively owned by those functions and they don't need to worry
>> anymore.
>>
>> So, passing global x to some function; inc/dec x around the function
>> call that it's passed to...? Then the stack has its own reference, and
>> the global reference can go away safely.
>
>
> This is my position too.
>
> There is another problem being discussed now, however, having to do with
> references to non-rc'd subcomponents of an Rc'd type.

Well you can't get to a subcomponent if not through it's owner.
If the question is about passing RC objects members to functions, then
the solution is the same as above, the stack needs a reference to the
parent before it can pass a pointer to it's member down the line for
the same reasons.
The trouble then is what if that member pointer escapes? Well I'd
imagine that it needs to be a scope pointer (I think we all agree RC
relies on scope). So a raw pointer to some member of an RC object must
be scope(*). That it can't escape, combined with knowledge that the
stack has a reference to it's owner, guarantees that it won't
disappear.


More information about the Digitalmars-d mailing list