A Refcounted Array Type
Steven Schveighoffer via Digitalmars-d
digitalmars-d at puremagic.com
Thu Feb 26 08:51:30 PST 2015
On 2/24/15 3:34 PM, Walter Bright wrote:
> On 2/24/2015 6:56 AM, Steven Schveighoffer wrote:
>> Actually, RCArray can never be allocated on GC, or you may corrupt
>> memory. count
>> may be non-null, and point at invalid memory when the dtor is called.
>
> Just set count to null after the delete.
No, the GC may have already deallocated the memory count points at, and
count is still non-null.
This is why you can never refer to GC-allocated memory in dtors, if they
can be called from the GC.
>> Only safe way to do this is to C malloc/free the count. And yes, at
>> that point,
>> you need atomics.
>
> No, RCArray is not intended for shared access between threads.
GC is what shares the access. For instance, if you have an RCArray in
one object that is being collected, it will attempt to decrement count.
However, that doesn't mean that another RCArray reference in the
originating thread won't also be trying to adjust count. In reality,
it's only shared between the GC-collection running thread and the
originating thread. I was not talking at all about making RCArray
shared-usable.
As talked about before, running dtors in the originating thread can
solve this problem.
-Steve
More information about the Digitalmars-d
mailing list