RCArray is unsafe

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Wed Mar 4 07:42:24 PST 2015


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




More information about the Digitalmars-d mailing list