RCArray is unsafe
Zach the Mystic via Digitalmars-d
digitalmars-d at puremagic.com
Mon Mar 2 17:23:22 PST 2015
On Monday, 2 March 2015 at 20:37:46 UTC, Walter Bright wrote:
> On 3/1/2015 12:51 PM, Michel Fortin wrote:
>> That's actually not enough. You'll have to block access to
>> global variables too:
>>
>> S s;
>>
>> void main() {
>> s.array = RCArray!T([T()]); // s.array's refcount is
>> now 1
>> foo(s.array[0]); // pass by ref
>> }
>> void foo(ref T t) {
>> s.array = RCArray!T([]); // drop the old s.array
>> t.doSomething(); // oops, t is gone
>> }
So with Andrei's solution, will s.array ever get freed, since s
is a global? I guess it *should* never get freed, since s is a
global and it will always exist as a reference.
Which makes me think about a bigger problem... when you opAssign,
don't you redirect the variable to a different instance? Won't
the destructor then destroy *that* instance (or not destroy it,
since it just got a +1 count) instead of the one most recently
decremented? How does it hold onto the instance to be destroyed?
More information about the Digitalmars-d
mailing list