RCArray is unsafe

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Wed Mar 4 09:22:15 PST 2015


On 3/4/15 10:42 AM, Andrei Alexandrescu wrote:
> On 3/4/15 12:55 AM, Ivan Timokhin wrote:
>> Excuse me if I miss something obvious, but:
>>
>>      void main()
>>      {
>>          auto arr = RCArray!int([0]);
>>          foo(arr, arr[0]);
>>      }
>>
>>      void foo(ref RCArray!int arr, ref int val)
>>      {
>>          {
>>              auto copy = arr; //arr's (and copy's) reference counts
>> are both 2
>>              arr = RCArray!int([]); // There is another owner, so arr
>>                                     // forgets about the old payload
>>          } // Last owner of the array ('copy') gets destroyed and happily
>>            // frees the payload.
>>          val = 3; // Oops.
>>      }
>
> That's a problem, thanks very much for pointing it out. -- Andrei

Again, I think this is an issue with the expectation of RCArray. You 
cannot *save* a ref to an array element, only a ref to the array itself, 
because you lose control over the reference count.

I don't think arr[0] should correctly bind to foo's second argument.

-Steve



More information about the Digitalmars-d mailing list